C++ 错误C2039是什么?在这种情况下它意味着什么?
由于以下错误,我无法编译代码:C++ 错误C2039是什么?在这种情况下它意味着什么?,c++,visual-c++,visual-studio-2013,compiler-errors,C++,Visual C++,Visual Studio 2013,Compiler Errors,由于以下错误,我无法编译代码: Error C2039: 'difference_type' : is not a member of 'Player' c:\program files (x86)\ microsoft visual studio 12.0\vc\include\xutility 373 1 Football Simulator" Error C2039: 'iterator_category' : is not a member of 'Player' c:\progr
Error C2039: 'difference_type' : is not a member of 'Player' c:\program files (x86)\
microsoft visual studio 12.0\vc\include\xutility 373 1 Football Simulator"
Error C2039: 'iterator_category' : is not a member of 'Player' c:\program files (x86)\
microsoft visual studio 12.0\vc\include\xutility 371 1 Football Simulator"
等等
这些消息对于如何修复代码或编译器遇到问题的地方没有任何价值,并且留下了我现在无法编译的代码。
我的代码有一个特定的问题,我得到了大量无用的编译器错误,其中一个错误是一个有用的错误,它告诉我实际的错误在哪里
这是我可以收集的代码。
作为参考,xutility的第371行如下
// TEMPLATE CLASS iterator_traits
template<class _Iter>
struct iterator_traits
{ // get traits from iterator _Iter
typedef typename _Iter::iterator_category iterator_category;
typedef typename _Iter::value_type value_type;
typedef typename _Iter::difference_type difference_type;
typedef difference_type distance_type; // retained
typedef typename _Iter::pointer pointer;
typedef typename _Iter::reference reference;
};
错误列表和输出中未显示任何有用信息,第5条消息指出:
1>team.cpp(13):参见正在编译的函数模板实例化“void std::fill(_-FwdIt,_-FwdIt,const _-Ty&)”的参考
我该如何识别这样的问题?我以前的方法是检查第一个错误并转到指定的行,但这一次不起作用
谢谢。请阅读 看起来您的代码正在使用模板。在模板实例化中,类的某个地方应该有
difference\u type
和iterator\u category
。这意味着要么您的类对象需要提供这些方法,要么您将错误的类型传递给模板函数或对象
检查类的所有引用/用法,尤其是在模板函数中。这将帮助您找到错误。正如我上面的评论所述,错误确实显示在Visual Studio输出窗口中,您只是因为某种原因没有看到它 错误说明如下:
1> team.cpp(13) : see reference to function template instantiation 'void std::fill<Player,int>(_FwdIt,_FwdIt,const _Ty &)' being compiled
现在大家都看到了,我们现在可以告诉你问题是什么。std::fill函数要求前两个参数使用迭代器,但您提供的是完整的对象,而不是迭代器
解决方法是这样做:
std::fill(acPlayer, acPlayer + nNumberOfPlayers, nAttributeValue);
指针具有与迭代器相同的特性,迭代器满足std::fill
所需的条件。最初的问题是std::fill
算法将操作符+
应用于迭代器,并且由于您传递了一个未定义++
的对象,因此发出了错误
因此,与你在文章中的说法相反,这些不是“无意义的编译器错误”。错误完全有道理。请形成一个链接,并将其内联发布到您的问题中。我已尽了最大努力,但不幸的是,由于我不知道问题的根源,我无法准确地向您提供该链接要求的内容。应该有一个包含错误的调用堆栈返回到您的代码。但是Johnny,生成MCVE会告诉您问题的根源。最小化的部分任务是删除程序中不重要的部分,直到错误消失。(对于产生错误来说是不重要的,而不是一般的程序。记住,一旦识别出错误,您将返回并修复原始程序。)当错误消失时,您已经识别出一个关键部分。保留它并开始移除其他东西。重复此操作,直到无法删除任何内容时错误才会消失。现在它是最小的,并且您有关于原因的额外线索。@JohnnyBravo我花时间下载并编译了您的代码。错误就在错误窗口中,您只是没有耐心去查看导致问题的代码行。请编译您的程序,并花时间浏览该日志——您将看到对调用
std::fill
的行的引用。既然我给了你们一个线索,请用这些信息更新你们的问题。我的代码没有使用模板。你们的代码可能没有使用模板。但它可能被传递给一个可以使用模板或STL的函数,或者期望该函数出现。您可以生成预处理的输出,并查看上述函数的引用。@JohnnyBravo您的代码正在使用模板。它使用的是std::fill
调用正好在后面。
Team::Team(int nNumberOfPlayers, int nAttributeValue){
acPlayer = new Player[nNumberOfPlayers];
std::fill(acPlayer[0], acPlayer[nNumberOfPlayers-1], nAttributeValue); // error
nTeamSize = nNumberOfPlayers;
}
1> team.cpp(13) : see reference to function template instantiation 'void std::fill<Player,int>(_FwdIt,_FwdIt,const _Ty &)' being compiled
//Test Slow version
Team::Team(int nNumberOfPlayers, int nAttributeValue){
acPlayer = new Player[nNumberOfPlayers];
std::fill(acPlayer[0], acPlayer[nNumberOfPlayers-1], nAttributeValue); // error
nTeamSize = nNumberOfPlayers;
}
std::fill(acPlayer, acPlayer + nNumberOfPlayers, nAttributeValue);