c – 为什么此模板在Xcode中有错误而在Visual Studio中没有错误
发布时间:2020-12-14 18:08:00 所属栏目:百科 来源:网络整理
导读:在C中使用模板时,我在 Xcode中收到错误.谁能告诉我有什么问题? 第一个版本在Xcode中报告错误,但在Visual Studio中报告错误. // Version 1: Error in Xcode,but not Visual Studiotemplatetypename LengthT,typename VertexT int MyGraphAlgorithm(...argume
在C中使用模板时,我在
Xcode中收到错误.谁能告诉我有什么问题?
第一个版本在Xcode中报告错误,但在Visual Studio中报告错误. // Version 1: Error in Xcode,but not Visual Studio template<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 Studio template<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是不明确的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |