C++ 错误 C2653“类”不是类或命名空间名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13273926/
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-27 17:08:18  来源:igfitidea点击:

C++ Error C2653 'Class' is not a class or namespace name

c++class

提问by Carlos Landeras

Good afternoon. I've started learning c++ and I am having and issue compiling my project. If you find some faulty code I would be glad if you tell me.

下午好。我已经开始学习 C++ 并且我在编译我的项目时遇到了问题。如果你发现一些错误的代码,如果你告诉我,我会很高兴。

I have the following definitions:

我有以下定义:

Utils.h

实用程序

#include "stdafx.h"
#include <string>
using namespace std;

class Utils
{
public:
static string GetStringFromInt (int number);
};

Utils.cpp

实用程序

#include "Utils.h"
#include <sstream>
#include <string>
using namespace std;

 string Utils::GetStringFromInt (int number)
{

    stringstream ss;
    ss << number;
    return ss.str();
}

and

Ping.h

平.h

#include "stdafx.h"
#include <string>

using namespace std;

class Ping
{
public:
    static int PingIt(int argc, char* argv[],string &mstime,string &ttl);   
};

Ping.cpp

ping文件

#include "Ping.h"
#include <string>
#include "icmpdefs.h"
#include <string>
#include <iostream>
#include <sstream>
#include <WinSock.h>
#include <Windows.h>
#include "Utils.h"
#pragma comment(lib,"wsock32.lib")
using namespace std;

int Ping::PingIt(int argc, char* argv[],string &mstime,string &ttl)
{


    // Check for correct command-line args
    if (argc < 2) {
        cerr << "usage: ping <host>" << endl;
        return 1;
    }

    // Load the ICMP.DLL
    HINSTANCE hIcmp = LoadLibrary("ICMP.DLL");
    if (hIcmp == 0) {
        cerr << "Unable to locate ICMP.DLL!" << endl;
        return 2;
    }

    // Look up an IP address for the given host name
    struct hostent* phe;
    if ((phe = gethostbyname(argv[1])) == 0) {
        cerr << "Could not find IP address for " << argv[1] << endl;
        return 3;
    }

    // Get handles to the functions inside ICMP.DLL that we'll need
    typedef HANDLE (WINAPI* pfnHV)(VOID);
    typedef BOOL (WINAPI* pfnBH)(HANDLE);
    typedef DWORD (WINAPI* pfnDHDPWPipPDD)(HANDLE, DWORD, LPVOID, WORD,
            PIP_OPTION_INFORMATION, LPVOID, DWORD, DWORD); // evil, no?
    pfnHV pIcmpCreateFile;
    pfnBH pIcmpCloseHandle;
    pfnDHDPWPipPDD pIcmpSendEcho;
    pIcmpCreateFile = (pfnHV)GetProcAddress(hIcmp,
            "IcmpCreateFile");
    pIcmpCloseHandle = (pfnBH)GetProcAddress(hIcmp,
            "IcmpCloseHandle");
    pIcmpSendEcho = (pfnDHDPWPipPDD)GetProcAddress(hIcmp,
            "IcmpSendEcho");
    if ((pIcmpCreateFile == 0) || (pIcmpCloseHandle == 0) || 
            (pIcmpSendEcho == 0)) {
        cerr << "Failed to get proc addr for function." << endl;
        return 4;
    }

    // Open the ping service
    HANDLE hIP = pIcmpCreateFile();
    if (hIP == INVALID_HANDLE_VALUE) {
        cerr << "Unable to open ping service." << endl;
        return 5;
    }

    // Build ping packet
    char acPingBuffer[64];
    memset(acPingBuffer, '\xAA', sizeof(acPingBuffer));
    PIP_ECHO_REPLY pIpe = (PIP_ECHO_REPLY)GlobalAlloc(
            GMEM_FIXED | GMEM_ZEROINIT,
            sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer));
    if (pIpe == 0) {
        cerr << "Failed to allocate global ping packet buffer." << endl;
        return 6;
    }
    pIpe->Data = acPingBuffer;
    pIpe->DataSize = sizeof(acPingBuffer);      

    // Send the ping packet
    DWORD dwStatus = pIcmpSendEcho(hIP, *((DWORD*)phe->h_addr_list[0]), 
            acPingBuffer, sizeof(acPingBuffer), NULL, pIpe, 
            sizeof(IP_ECHO_REPLY) + sizeof(acPingBuffer), 5000);
    if (dwStatus != 0) {
        cout << "Addr: " <<
                int(LOBYTE(LOWORD(pIpe->Address))) << "." <<
                int(HIBYTE(LOWORD(pIpe->Address))) << "." <<
                int(LOBYTE(HIWORD(pIpe->Address))) << "." <<
                int(HIBYTE(HIWORD(pIpe->Address))) << ", " <<
                "RTT: " << int(pIpe->RoundTripTime) << "ms, " <<
                "TTL: " << int(pIpe->Options.Ttl) << endl;

        mstime = Utils::GetStringFromInt((pIpe->RoundTripTime));
        ttl = Utils::GetStringFromInt(int(pIpe->Options.Ttl));
    }
    else {
        cerr << "Error obtaining info from ping packet." << endl;
    }

    // Shut down...
    GlobalFree(pIpe);
    FreeLibrary(hIcmp);
    return dwStatus;
}

When I Compile the project I get:

当我编译项目时,我得到:

Error 1 error C2653: 'Ping' : is not a class or namespace name c:\users\clanderasm\documents\visual studio 2010\projects\landetestconsole\landecplusconsole\ping.cpp 14 1 LandeCplusConsole

错误 1 ​​错误 C2653:“Ping”:不是类或命名空间名称 c:\users\clanderasm\documents\visual studio 2010\projects\landetestconsole\landecplusconsole\ping.cpp 14 1 LandeCplusConsole

I've read sometimes this error is thrown because you dont include "stdafx.h" on the first #include but I already changed it.

我读过有时会抛出此错误,因为您没有在第一个 #include 中包含“stdafx.h”,但我已经更改了它。

If you could tell me something more I would be glad

如果你能告诉我更多的话我会很高兴

采纳答案by Chris R.

I couldn't reproduce your error with the code you gave, but I tried on VS2008 and some of the warning it raised make me think it could very much be due to your precompiled header not being included in sources. And there is a couple of other problems I can see that will surely cause you problems later:

我无法用您提供的代码重现您的错误,但我在 VS2008 上进行了尝试,并且它引发的一些警告让我认为这很可能是由于您的预编译头文件未包含在源代码中。我可以看到还有一些其他问题肯定会在以后给您带来问题:

  • Don't include precompiled header in .h. (Well you should even avoid including anything in your .h unless absolutely necessary). The precompiled header (at least in visual way of doing things) is meant to be included first in each cpp files (not .h). If you don't do so it will raise warnings for each includes you have like:

    warning C4627: '#include "Ping.h"': skipped when looking for precompiled header use <- here you go your Point class is no longer defined!

    and finally an error fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?.

    It's actually messages I get when compiling your code on VS2008, maybe on VS2010 you have your error about Point not being defined in addition to those. When sorting out precompiled header problems it compiles fine (see next point)

  • Using a precompiled header does not mean the .h used to build it will be automatically included in all your sources. To do so, you have to change some project setting: right clickon your project -> Properties, in the left panel, expand Configuration Properties -> C/C++ -> Advanced. Here on the list on the right you should see Force Includes. Type here stdafx.h, and voilà, you won't have to put it manually in each and every new .cpp you add to your project. Beware that you have to do that for all configuration (combo box on the top written "Configuration : Active(Debug)"

  • 不要在 .h 中包含预编译头文件。(好吧,除非绝对必要,否则您甚至应该避免在 .h 中包含任何内容)。预编译的头文件(至少以可视化的方式)意味着首先包含在每个 cpp 文件(不是 .h)中。如果您不这样做,它将为您拥有的每个包含发出警告:

    警告 C4627:'#include "Ping.h"':在查找预编译头使用时跳过 <- 在这里,您的 Point 类不再定义!

    最后一个错误致命错误 C1010:在查找预编译头时意外的文件结尾。您是否忘记在源代码中添加“#include“stdafx.h””?。

    这实际上是我在 VS2008 上编译代码时收到的消息,也许在 VS2010 上,除了这些之外,您还有关于 Point 未定义的错误。当整理出预编译头问题时,它编译得很好(见下一点)

  • 使用预编译头并不意味着用于构建它的 .h 将自动包含在您的所有源中。为此,您必须更改一些项目设置:右键单击您的项目 -> Properties,在左侧面板中,展开Configuration Properties -> C/C++ -> Advanced。在右侧的列表中,您应该看到Force Includes。在此处键入 stdafx.h,瞧,您不必将其手动放入您添加到项目中的每个新 .cpp 中。请注意,您必须对所有配置执行此操作(顶部写有“配置:活动(调试)”的组合框)

Apologies, still a VS2008 screen, hope it's the same on VS2010 enter image description here

抱歉,还是VS2008的画面,希望VS2010也一样 在此处输入图片说明

  • Guard your headers. You should put gards on your headers to avoid multiple definitions of your classes when multiple include of the same .h happens (and it will). You can do it 2 ways: the define method, and the pragma once method. The pragma once is not standard but compiles faster on Visual, so you can eventually mix the 2 ways.
  • 守卫你的头。当多个包含相同的 .h 发生(并且会发生)时,您应该在您的标题上放置 gards 以避免您的类的多个定义。您可以通过两种方式实现:define 方法和 pragma once 方法。pragma once 不是标准的,但在 Visual 上编译速度更快,因此您最终可以混合两种方式。

myheader.h using pragma once:

myheader.h 使用编译指示一次:

#pragma once
class MyClass
{
    //<some definitions>
};

myheader.h using defines:

myheader.h 使用定义:

#ifndef __MYHEADER_H
#define __MYHEADER_H
class MyClass
{
    //<some definitions>
};
#endif

myheader.h using both:

myheader.h 使用两者:

#pragma once
#ifndef __MYHEADER_H
#define __MYHEADER_H
class MyClass
{
    //<some definitions>
};
#endif
  • It has already been said, but avoid the use of "using" in headersbecause it will spread. I myself avoid the use of "using" everywhere.
  • 已经说过了,但是避免在标题中使用“使用”,因为它会传播。我自己避免在任何地方使用“使用”。