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

前端之家收集整理的这篇文章主要介绍了c – 为什么此模板在Xcode中有错误而在Visual Studio中没有错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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是不明确的.

猜你在找的Xcode相关文章