我知道这个问题之前已经被问过并回答了,但是没有一个解决方案似乎对我有用,而且我的编译器对这个错误表现得非常奇怪。
当我尝试编译我的代码时,我会收到许多错误,例如:
Error 1 error C2653: 'TargetList' : is not a class or namespace name c:\projects\arcturus\augmentedreality\targetlist.cpp 5 1 AugmentedReality
Error 2 error C2065: 'Target' : undeclared identifier c:\projects\arcturus\augmentedreality\targetlist.cpp 5 1 AugmentedReality
Error 3 error C2146: syntax error : missing ')' before identifier 'target' c:\projects\arcturus\augmentedreality\targetlist.cpp 5 1 AugmentedReality
Error 4 error C2059: syntax error : ')' c:\projects\arcturus\augmentedreality\targetlist.cpp 5 1 AugmentedReality
Error 5 error C2143: syntax error : missing ';' before '{' c:\projects\arcturus\augmentedreality\targetlist.cpp 6 1 AugmentedReality
Error 6 error C2447: '{' : missing function header (old-style formal list?) c:\projects\arcturus\augmentedreality\targetlist.cpp 6 1 AugmentedReality
我之前在编译项目时遇到过这种错误,但它神秘地消失了。我试图解决这个问题,过了一会儿它恢复了我所有的变化后才开始工作。
我认为这可能是我的预编译头文件的问题,因为在我尝试修复错误后我的PCH无法正常工作时弹出此错误。
这是我的代码(我知道它不是设计得很好,只是试图让它在当下工作:P):
的StdAfx.h
#pragma once
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
Target.h
#pragma once
#include "Position.h"
#include <string>
#include <vector>
class Target
{
public:
Target();
Target(std::string shortName, std::string longName, Position position);
~Target();
bool UpdateTargetData(Position currentPosition);
std::string mShortName;
std::string mLongName;
Position mPosition;
double mDistance;
double mHorizontalBearing;
double mVerticalBearing;
};
Target.cpp
#include "Target.h"
#include "stdafx.h"
bool Target::UpdateTargetData(Position currentPosition)
{
mDistance = currentPosition.GetDistance(mPosition);
mHorizontalBearing = currentPosition.GetHorizontalBearing(mPosition);
mVerticalBearing = currentPosition.GetVerticalBearing(mPosition);
return true;
}
TargetList.h
#pragma once
#include "Target.h"
class TargetList
{
public:
TargetList();
~TargetList();
bool AddTarget(Target target);
bool GetTarget(std::string shortName, Target& returnTarget);
bool RemoveTarget(std::string shortName);
private:
std::vector<Target> mTargets;
};
TargetList.cpp
#include "TargetList.h"
#include "Target.h"
#include "stdafx.h"
bool TargetList::AddTarget(Target target)
{
if (GetTarget(target.mShortName, Target()) != false)
{
mTargets.push_back(target);
return true;
}
return false;
}
bool TargetList::GetTarget(std::string shortName, Target& returnTarget)
{
std::vector<Target>::iterator iterator;
for (iterator = mTargets.begin(); iterator < mTargets.end(); iterator++)
{
if ((*iterator).mShortName == shortName)
{
returnTarget = (*iterator);
return true;
}
}
return false;
}
bool TargetList::RemoveTarget(std::string shortName)
{
std::vector<Target>::iterator iterator;
for (iterator = mTargets.begin(); iterator < mTargets.end(); iterator++)
{
if ((*iterator).mShortName == shortName)
{
mTargets.erase(iterator);
return true;
}
}
return false;
}
答案 0 :(得分:25)
PCH(即stdafx.h
)应首先包含在.cpp
文件中。所以这样做:
#include "stdafx.h" //this should be included first!
#include "TargetList.h"
#include "Target.h"
请参阅以下主题: