iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 为什么此模板在Xcode中有错误而在Visual Studio中没有错误?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_616_2@

概述

在C中使用模板时,我在 Xcode中收到错误.谁能告诉我有什么问题? 第一个版本在xcode中报告错误,但在Visual studio中报告错误. // Version 1: Error in Xcode, but not Visual studio template<typename LengthT, typENAME vertexT> int MyGraphAlgorithm(...argu
@H_616_2@
@H_616_2@ @H_616_2@
@H_616_2@
在C中使用模板时,我在 Xcode中收到错误.谁能告诉我有什么问题? @H_197_19@ @H_197_19@第一个版本在xcode中报告错误,但在Visual studio中报告错误.

@H_197_19@
// 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);
}
@H_197_19@第二个没有错误.不同之处在于模板化typedef中模板参数LengthT的使用.

@H_197_19@
// 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);
}
@H_616_2@

解决方法

出错的原因是编译器不知道graph_Traits< Graph> :: vertex_descriptor是什么.它是静态成员还是类型?如果它是一种类型,那么你必须这样说: @H_197_19@ @H_197_19@
typename graph_Traits<Graph>::vertex_descriptor
@H_197_19@编译器不够聪明,无法自行解决的原因是因为LengthT是模板参数.它可以是任何东西,因此在模板声明时,编译器无法判断它的值是什么,因此typedef是不明确的.

@H_616_2@ @H_616_2@
@H_616_2@
@H_616_2@@H_616_2@

大佬总结

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

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。