C++错误 C2653 'Class' 不是一个类或命名空间名称。

8
下午好。我开始学习c++,但编译项目时出现了问题。如果您发现有错误的代码,请告诉我,谢谢。
以下是我的定义:
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

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

using namespace std;

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

Ping.cpp

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

当我编译项目时,出现以下错误: 错误 1 错误 C2653: 'Ping' : 不是类或命名空间名称 c:\users\clanderasm\documents\visual studio 2010\projects\landetestconsole\landecplusconsole\ping.cpp 14 1 LandeCplusConsole 有时会因为在第一个#include中未包含"stdafx.h"而引发此错误,但我已经更改了它。
如果您能告诉我更多信息,我会很高兴。

3
还有其他的警告吗?也许是因为找不到 #include "Ping.h"。另外,在头文件中千万不要加上 using namespace std;。永远不要这样做。 - Collin
2
关于stdafx.h的包含问题,它应该作为你的源代码文件中第一个#include出现,而不是放在头文件中。每个头文件都应该包含其所依赖的本地定义,或者如果不需要文件独立性,则应包含任何在编译相关源文件时尚未被stdafx.h引入的内容。 - WhozCraig
除了WhosCraig所说的,stdafx.h只是Visual Studio中预编译头文件的默认名称。没有规定它必须被称为这个名字,也没有规定它需要在C++项目中存在或者被包含在所有文件中,这只是Visual Studio默认为您设置的,除非您从空项目开始。由于它被包含在PCH中,它将自动包含在所有源文件中,但最好还是手动在源文件中#include它 - 只是在源文件中而不是头文件中。 - Ben Hymers
请不要在头文件中使用 using namespace - John Dibling
不要包含超过必要的头文件。 - Sebastian Mach
显示剩余4条评论
1个回答

9

根据你提供的代码,我无法重现错误。但是我在VS2008上尝试了一下,发现它引发的一些警告让我认为这很可能是由于未在源文件中包含预编译头文件导致的。此外,我还看到其他一些问题,这些问题以后肯定会给你造成麻烦:

  • 不要在.h文件中包含预编译头文件(除非绝对必要)。预编译头文件(至少在Visual Studio的做法中)应该首先包含在每个cpp文件(而不是.h文件)中。如果你不这样做,它将为每个包含文件引发警告,例如:

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

    最终出现错误致命错误 C1010:在寻找预编译头时意外遇到文件结尾。你是否忘记在源文件中添加“#include“stdafx.h”?

    这实际上是我在VS2008上编译你的代码时收到的消息,也许在VS2010中,你还会遇到关于Point未定义的错误。解决预编译头文件的问题后,它可以编译成功(请参阅下一条说明)

  • 使用预编译头文件并不意味着用于构建它的.h文件会自动包含在所有源文件中。要这样做,你必须更改某些项目设置:右键单击你的项目 -> 属性,在左侧面板中,展开配置属性 -> C/C++ -> 高级选项。在右侧列表中,你应该看到强制包含。在这里输入stdafx.h,完成后,你就不必在每个新添加到你的项目中的.cpp文件中手动添加它了。请注意,你必须为所有配置(顶部写“配置:Active(Debug)”的组合框)进行此操作。

抱歉,这仍然是VS2008屏幕,希望在VS2010上是相同的。 enter image description here

  • 保护你的头文件。你应该在头文件上加入保护,以避免在多次包含相同.h文件时多次定义类(这种情况确实存在)。 你可以通过#define方法和#pragma once方法来实现。虽然#pragma once方法不是标准方法,但在Visual Studio上编译速度更快,因此你可以混合使用这两种方法。

myheader.h使用#pragma once:

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

使用定义的myheader.h:

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

使用myheader.h文件的两种方法:

#pragma once
#ifndef __MYHEADER_H
#define __MYHEADER_H
class MyClass
{
    //<some definitions>
};
#endif
  • 已经有人提到过了,但避免在标题中使用"using",因为它会传播。我自己也会避免在任何地方使用"using"。

非常感谢您的帮助。您应该得到这个被接受的答案。这是一个Visual Studio的故障。我从解决方案和磁盘中删除了.h和.cpp文件,然后它就编译成功了。 - Carlos Landeras

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接