【发布时间】:2015-04-01 02:33:26
【问题描述】:
由于以下错误,我无法编译我的代码:
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"
等等。
这些消息对于如何修复代码或编译器遇到问题并留下我现在无法编译的代码没有任何价值。 我的代码存在一个特定问题,我收到一连串无用的编译器错误,其中一个有用的错误告诉我实际错误在哪里
https://www.dropbox.com/s/jr2lmqwvfurpnok/Football%20Simulator.rar?dl=0 这是我可以收集的代码。 作为参考,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;
};
这是我的 stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <algorithm>
#include <cassert>
#include <string>
#include <time.h>
这是我的 Player.h,非常好。
#pragma once
#include "PlayerAttributes.h"
enum Position{
GK,
FB,
CB,
DM,
CM,
FW,
MAX_POSITIONS
};
class Player
{
PlayerAttributes Attributes;
std::string m_sName;
int m_nAge;
Position m_ePosition;
std::string e_sTeamName;
public:
Player();
Player(int nDesiredAttributeValues);
//
Player(std::string sName, int nAge, Position ePosition);
Player(std::string sName, int nAge, Position ePosition, int nDesiredAttributeValues);
~Player();
friend std::ostream& operator<< (std::ostream &out, Player &cPlayer);
};
编辑: 如下所述,实际错误在于文件 team.cpp
Team::Team(int nNumberOfPlayers, int nAttributeValue){
acPlayer = new Player[nNumberOfPlayers];
std::fill(acPlayer[0], acPlayer[nNumberOfPlayers-1], nAttributeValue); // error
nTeamSize = nNumberOfPlayers;
}
错误列表和输出中没有出现任何有用的信息,第 5 条消息指出:
1> team.cpp(13) : 请参阅正在编译的函数模板实例化 'void std::fill(_FwdIt,_FwdIt,const _Ty &)' 的参考
我应该如何识别这样的问题?我之前的方法是检查第一个错误并转到说明的行,但这一次不起作用。
谢谢。
【问题讨论】:
-
请形成一个MCVE 并将其发布在您的问题中。
-
我尽了最大努力,但不幸的是,我不知道问题出在哪里,所以我无法准确地向您提供该链接所要求的内容。
-
应该有一个调用堆栈,错误会导致返回到您的代码。
-
但是约翰尼,生成一个 MCVE 告诉你问题出在哪里。使其最小化的部分任务是删除程序中不重要的部分,直到错误消失。 (对产生错误无关紧要,而不是对您的程序一般而言。请记住,一旦您发现错误,您将返回并修复您的原始程序。)当错误消失时,您已经确定了一个关键部分。保留它并开始删除其他东西。重复,直到您无法删除任何内容而错误消失。 现在它已经很少了,而且你有更多关于原因的线索。
-
@JohnnyBravo 我花时间下载并编译了您的代码。错误就在错误窗口中,您只是没有耐心转到导致问题的代码行。请编译您的程序,并花时间浏览该日志——您将看到对您调用
std::fill的行的引用。既然我给了你一个线索,请用这些信息更新你的问题。
标签: c++ visual-c++ visual-studio-2013 compiler-errors