本文介绍了C ++错误C2653'类'不是类或命名空间名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 今天下午好。我开始学习c ++,我有编译我的项目和问题。 如果你发现一些错误的代码,我会很高兴,如果你告诉我。 我有以下定义: Utils.h #includestdafx.h #include< string> using namespace std; class Utils { public: static string GetStringFromInt(int number); }; Utils.cpp #includeUtils.h #include< sstream> #include< string> using namespace std; string Utils :: GetStringFromInt(int number) { stringstream ss; ss<<数; return ss.str(); } 和 Ping.h #includestdafx.h #include< string> ; using namespace std; class Ping { public: static int PingIt(int argc,char * argv [],string& mstime,string& ttl); }; Ping.cpp #includePing.h #include< string> #includeicmpdefs.h #include< string> #include< iostream> #include< sstream> #include< WinSock.h> #include< Windows.h> #includeUtils.h #pragma comment(lib,wsock32.lib) using namespace std; int Ping :: PingIt(int argc,char * argv [],string& mstime,string& ttl) { //检查正确的命令行参数 if(argc cerr<< usage:ping< host> << endl; return 1; } //加载ICMP.DLL HINSTANCE hIcmp = LoadLibrary(ICMP.DLL); if(hIcmp == 0){ cerr<< 无法找到ICMP.DLL! << endl; return 2; } //查找给定主机名的IP地址 struct hostent * phe; if((phe = gethostbyname(argv [1]))== 0){ cerr< 找不到<< argv [1]<< endl; return 3; } //获取ICMP.DLL中的函数的句柄,我们需要 typedef HANDLE(WINAPI * pfnHV)(VOID); typedef BOOL(WINAPI * pfnBH)(HANDLE); typedef DWORD(WINAPI * pfnDHDPWPipPDD)(HANDLE,DWORD,LPVOID,WORD, PIP_OPTION_INFORMATION,LPVOID,DWORD,DWORD); //邪恶,没有? 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< 无法获取proc addr的函数。 << endl; $ b $ p return 4; } //打开ping服务 HANDLE hIP = pIcmpCreateFile(); if(hIP == INVALID_HANDLE_VALUE){ cerr<< 无法打开ping服务。 << endl; return 5; } //构建ping数据包 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<< 无法分配全局ping数据包缓冲区。 << endl; return 6; } pIpe-> Data = acPingBuffer; pIpe-> DataSize = sizeof(acPingBuffer); //发送ping数据包 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<< 从ping数据包获取信息时出错。 << endl; } //关闭... GlobalFree(pIpe); FreeLibrary(hIcmp); return dwStatus; } 当我编译项目时,我得到: 错误1错误C2653:'Ping':不是类或命名空间名称c:\users\clanderasm\documents\visual studio 2010\projects\landetestconsole\ landecplusconsole\ping.cpp 14 1 LandeCplusConsole 我看过有时会抛出这个错误,因为你不包括stdafx.h 如果你能告诉我更多,我会很高兴。解决方案我无法用你给的代码重现你的错误,但我试过VS2008和一些警告,它提出让我认为这可能是由于你的预编译头不包括在源中。还有几个其他问题,我可以看到,一定会导致你的问题后来: 在.h中包含预编译头。。 (你应该避免在你的.h中包括任何东西,除非绝对必要)。预编译头(至少以视觉方式做事情)意味着首先包含在每个cpp文件(不是.h)中。如果您不这样做,则会为每个包含您的内容产生警告: 警告C4627 :#includePing.h ':当寻找预编译头使用时跳过 最后错误致命错误C1010 :查找预编译头时意外的文件结束。你忘了添加#includestdafx.h'到你的源吗? 这是我在VS2008上编译代码时得到的消息,也许在VS2010上,你有关于Point的错误,除了这些。 使用预编译头并不意味着用于构建它的.h将被自动包含在内在所有的来源。为此,您必须更改项目设置:在项目上点击右键 - > 属性,在左侧面板中展开配置属性 - > C / C ++ - >高级。在右侧列表中,您应该会看到强制包含。在这里输入stdafx.h和voilà,你不必手动将它放在你添加到项目中的每个新的.cpp文件中。请注意,您必须为所有配置(顶部写入配置:活动(调试)上的组合框 Apologies,仍然是VS2008屏幕,希望它在VS2010上是一样的 保护头文件,您应该在头文件中放置gards以避免多个定义你可以做两种方法:define方法和pragma once方法 pragma一次不是标准的,但在Visual上编译得更快,所以你 myheader.h使用pragma一次: #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 { //< ;一些定义> }; #endif strong>避免在标头中使用使用,因为它会传播。我自己避免在任何地方使用使用。 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.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();}andPing.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;}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 LandeCplusConsoleI've read sometimes this error is thrown because you dont include "stdafx.h"on the first #include but I already changed it.If you could tell me something more I would be glad 解决方案 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: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 click on 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)"Apologies, still a VS2008 screen, hope it's the same on VS2010Guard 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.myheader.h using pragma once:#pragma onceclass MyClass{ //<some definitions>};myheader.h using defines:#ifndef __MYHEADER_H#define __MYHEADER_Hclass MyClass{ //<some definitions>};#endifmyheader.h using both:#pragma once#ifndef __MYHEADER_H#define __MYHEADER_Hclass MyClass{ //<some definitions>};#endifIt has already been said, but avoid the use of "using" in headers because it will spread. I myself avoid the use of "using" everywhere. 这篇关于C ++错误C2653'类'不是类或命名空间名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 09:19