c – 为什么此模板在Xcode中有错误而在Visual Studio中没有错误?

c – 为什么此模板在Xcode中有错误而在Visual Studio中没有错误?,第1张

概述在C中使用模板时,我在 Xcode中收到错误.谁能告诉我有什么问题? 第一个版本在Xcode中报告错误,但在Visual Studio中报告错误. // Version 1: Error in Xcode, but not Visual Studiotemplate<typename LengthT, typename VertexT> int MyGraphAlgorithm(...argu 在C中使用模板时,我在 Xcode中收到错误.谁能告诉我有什么问题?

第一个版本在Xcode中报告错误,但在Visual Studio中报告错误.

// Version 1: Error in Xcode,but not Visual Studiotemplate<typename LengthT,typename VertexT> int MyGraphAlgorithm(...arguments omitted...){  using namespace boost;  typedef property<vertex_distance_t,LengthT> VertextPropertIEs_t;  typedef adjacency_List<vecS,vecS,directedS,VertextPropertIEs_t> Graph;  // In next line Xcode reports: "error: expected `;' before 'vertexInitial'"  graph_traits<Graph>::vertex_descriptor vertexInitial(100);}

第二个没有错误.不同之处在于模板化typedef中模板参数LengthT的使用.

// Version 2: No error in Xcode or Visual Studiotemplate<typename LengthT,typename VertexT> int MyGraphAlgorithm(...arguments omitted...){  using namespace boost;  // In the following line,LengthT has been changed to int  typedef property<vertex_distance_t,int> VertextPropertIEs_t;  typedef adjacency_List<vecS,VertextPropertIEs_t> Graph;  graph_traits<Graph>::vertex_descriptor  vertexInitial(100);}
解决方法 出错的原因是编译器不知道graph_traits< Graph> :: vertex_descriptor是什么.它是静态成员还是类型?如果它是一种类型,那么你必须这样说:

typename graph_traits<Graph>::vertex_descriptor

编译器不够聪明,无法自行解决的原因是因为LengthT是模板参数.它可以是任何东西,因此在模板声明时,编译器无法判断它的值是什么,因此typedef是不明确的.

总结

以上是内存溢出为你收集整理的c – 为什么此模板在Xcode中有错误而在Visual Studio中没有错误?全部内容,希望文章能够帮你解决c – 为什么此模板在Xcode中有错误而在Visual Studio中没有错误?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1031317.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存