下午好。我已经开始学习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错误C2653:'Ping':不是类或命名空间名称c:\ users \ clanderasm \ documents \ visual studio 2010 \ projects \ landetestconsole \ landecplusconsole \ ping.cpp 14 1 LandeCplusConsole
我有时会读到这个错误,因为你不包括“stdafx.h” 在第一个#include但我已经改变了它。
如果你能告诉我更多我会很高兴
答案 0 :(得分:7)
我无法使用您提供的代码重现您的错误,但我尝试了VS2008并且它引发的一些警告让我认为这可能是因为您的预编译头文件未包含在源代码中。还有一些我可以看到的其他问题,这些问题肯定会在以后引起你的问题:
不要在.h 中包含预编译的标头。 (除非绝对必要,否则你甚至应该避免在.h中包含任何内容)。预编译头(至少以可视方式处理)意味着首先包含在每个cpp文件中(而不是.h)。如果您不这样做,它会针对您拥有的每个包含提出警告:
警告C4627 :'#include“Ping.h”':在查找预编译头时使用跳过&lt; - 这里你不再定义你的Point类了!
最后出现错误致命错误C1010 :查找预编译头时意外结束文件。您是否忘记在源代码中添加“#include”stdafx.h“'?
这实际上是我在VS2008上编译你的代码时得到的消息,也许在VS2010上你有关于Point之外没有被定义的错误。在整理出预编译的头文件问题时,它编译得很好(参见下一点)
使用预编译的标头并不意味着用于构建它的.h将自动包含在所有源中。为此,您必须更改一些项目设置:右键单击项目 - &gt; 属性,在左侧面板中,展开配置属性 - &gt; C / C ++ - &gt;高级即可。在右侧列表中,您应该看到强制包含。在这里输入stdafx.h,并且你不必手动将它添加到你添加到项目中的每个新的.cpp中。请注意,您必须为所有配置执行此操作(顶部的组合框写为“配置:活动(调试)”
道歉,仍然是VS2008屏幕,希望它在VS2010上是一样的
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
{
//<some definitions>
};
#endif