我知道這個問題以前已經被問及過,但是沒有一個解決方案似乎對我有用,而且我的編譯器在這個錯誤中表現得很奇怪。不是類或名稱空間名稱
當我嘗試編譯我的代碼,我得到了許多錯誤,如這些:
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;
}
:
看到這些主題的測試?另外,'#include「stdafx.h」'應該放在最上面。 –