不是类或命名空间名称

发布于 11-29 04:27 字数 3697 浏览 1 评论 0原文

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

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

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;
}

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.

Here is my code (and I know it isn't that well designed, just trying to get it to work at the moment :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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美煞众生2024-12-06 04:27:28

PCH(即stdafx.h)应首先包含在.cpp 文件中。因此,请执行以下操作:

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

请参阅以下主题:

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

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

See these topics:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文