gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:51:59 33 4
gpt4 key购买 nike

下午好。我已经开始学习 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();
}

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 ​​error C2653: 'Ping' : 不是类或命名空间名称 c:\users\clanderasm\documents\visual studio 2010\projects\landetestconsole\landecplusconsole\ping.cpp 14 1 LandeCplusConsole

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

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

最佳答案

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

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

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

    最后是一个错误fatal error C1010: unexpected end of file while looking for precompiled header。您是否忘记将“#include “stdafx.h””添加到您的源代码中?。

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

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

抱歉,还是VS2008的画面,希望VS2010也一样 enter image description here

  • 保护您的 header 。当多次包含相同的 .h 发生时(它会发生),你应该在你的标题上放置 gards 以避免你的类的多个定义。您可以通过两种方式实现:define 方法和 pragma once 方法。pragma once 不是标准的,但在 Visual 上编译速度更快,因此您最终可以混合使用这两种方式。

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

#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”,因为它会传播开来。我自己避免在任何地方使用“使用”。

关于C++ 错误 C2653 'Class' 不是类或命名空间名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13273926/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com