C++ 不是类或命名空间名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7050166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:24:40  来源:igfitidea点击:

is not a class or namespace name

c++classprecompiled-headers

提问by Nick Taylor

I know this question has been asked and answered before, but none of the solutions seem to have worked for me, and my compiler is acting really weird with this error.

我知道之前有人问过并回答过这个问题,但似乎没有一个解决方案对我有用,而且我的编译器对这个错误表现得很奇怪。

When I try and compile my code I get numerous errors such as these:

当我尝试编译我的代码时,我收到了许多错误,例如:

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

I encountered this kind of error when compiling my project before, but it mystically disappeared. I was trying to fix the problem, and after a while it just started to work again after I reverted all of my changes.

之前编译我的项目时遇到过这种错误,但是莫名其妙的就消失了。我试图解决这个问题,过了一会儿,在我恢复所有更改后,它又开始工作了。

I think this may be a problem with my precompiled header, as this error popped up after I had tried to fix an error with my PCH not working properly.

我认为这可能是我的预编译头文件的问题,因为在我尝试修复 PCH 无法正常工作的错误后,会弹出此错误。

Here is my code (and I know it isn't that well designed, just trying to get it to work at the moment :P):

这是我的代码(我知道它设计得不是那么好,现在只是想让它工作:P):

StdAfx.h

标准文件

#pragma once

#define _USE_MATH_DEFINES
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

Target.h

目标.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

目标列表.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

目标列表.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;
}

回答by Nawaz

PCH (i.e stdafx.h) should be included first in the .cppfile. So do this:

PCH(即stdafx.h)应首先包含在.cpp文件中。所以这样做:

#include "stdafx.h"     //this should be included first!
#include "TargetList.h"
#include "Target.h"

See these topics:

请参阅这些主题: