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();
}
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;
}
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
I'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 VS2010
myheader.h using pragma once:
#pragma once
class MyClass
{
//<some definitions>
};
myheader.h using defines:
#ifndef __MYHEADER_H
#define __MYHEADER_H
class MyClass
{
//<some definitions>
};
#endif
myheader.h using both:
#pragma once
#ifndef __MYHEADER_H
#define __MYHEADER_H
class MyClass
{
//<some definitions>
};
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With