-
June 6th, 2013, 05:04 PM
#1
Error in implenting class from .h into .cpp
I've been stressing on finding these errors that my compiler is giving me in this code.
ping.h
Code:
//=========================
//include guard
#ifndef __PING_H__
#define __PING_H__
//==========================
//==========================
//included dependencies
#include <iostream>
#include <string>
#include <windows.h>
//==========================
class Ping
{
private:
string host;
bool repeater;
public:
//============================================================
// 3x constructors
Ping()
{
host = "127.0.0.1";
repeater = false;
}
Ping(string set = "127.0.0.1")
:host(set)
{
host = set;
repeater = false;
}
Ping(host = "127.0.0.1", bool setRepeat = false)
:host(set), repeater(setRepeat)
{
host = set;
repeater = setRepeat;
}
//=============================================================
//Methods
void PingHost()
{
do{
cout << "Host: " + host;
system (("ping" + host).c_str());
}while(repeater);
}
void setRepeater(bool repeat = false)
:repeater(repeat)
{
repeater = repeat;
}
void setHost(string myHost = "127.0.0.1")
:host(myHost)
{
host = myHost;
}
//=============================================================
//destructor
~Ping()
{
host = "127.0.0.1";
repeater = false;
}
};
#endif // PING_H
ping.cpp
Code:
#include "ping.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
//"216.52.241.254"
string hostA = "216.52.241.254";
char reply = 'n';
Ping *myPing = new Ping(hostA, false);
cout << "Repeat calls? 'y' or 'n'";
cin >> reply;
if(reply == 'n' || reply == 'N')
myPing->setRepeater(0);
else if(reply == 'y' || reply == 'Y')
myPing->setRepeater(1);
else
myPing->setRepeater(0);
myPing->Ping;
delete myPing; //Changed from ~ping();
return 0;
}
Error List:
Code:
ping.cpp:1: In file included from ..\PingHost\ping.cpp:1:0:
ping.h:17: error: 'string' does not name a type
ping.h:27: error: expected ')' before 'set'
ping.h:34: error: expected ')' before '=' token
ping.h:55: error: 'string' has not been declared
ping.h:55: error: default argument for parameter of type 'int' has type 'const char [10]'
ping.h:-1: In constructor 'Ping::Ping()':
ping.h:24: error: 'host' was not declared in this scope
ping.h:-1: In member function 'void Ping::PingHost()':
ping.h:45: error: 'cout' was not declared in this scope
ping.h:45: suggested alternative:
ping.h:9: In file included from ..\PingHost\ping.h:9:0,
ping.cpp:1: from ..\PingHost\ping.cpp:1:
c:\qt\qt5.0.2\tools\mingw\lib\gcc\i686-w64-mingw32\4.7.2\include\c++\iostream:62: note: 'std::cout'
ping.cpp:1: In file included from ..\PingHost\ping.cpp:1:0:
ping.h:45: error: 'host' was not declared in this scope
ping.h:-1: In member function 'void Ping::setRepeater(bool)':
ping.h:51: error: only constructors take member initializers
ping.h:-1: In member function 'void Ping::setHost(int)':
ping.h:56: error: only constructors take member initializers
ping.h:56: error: class 'Ping' does not have any field named 'host'
ping.h:58: error: 'host' was not declared in this scope
ping.h:-1: In destructor 'Ping::~Ping()':
ping.h:64: error: 'host' was not declared in this scope
ping.cpp:-1: In function 'int main()':
ping.cpp:12: error: no matching function for call to 'Ping::Ping(std::string&, bool)'
ping.cpp:12: candidates are:
ping.cpp:1: In file included from ..\PingHost\ping.cpp:1:0:
ping.h:22: Ping::Ping()
ping.h:22: note: candidate expects 0 arguments, 2 provided
ping.h:14: Ping::Ping(const Ping&)
ping.h:14: note: candidate expects 1 argument, 2 provided
ping.cpp:24: error: invalid use of 'Ping::Ping'
ping.cpp:25: error: 'ping' was not declared in this scope
Last edited by Zyrion; June 8th, 2013 at 01:49 AM.
-
June 6th, 2013, 05:07 PM
#2
Re: Error in implenting class from .h into .cpp
Oh! I didn't scroll down enough...
You need to qualify things like string with std::string in the header
Last edited by S_M_A; June 6th, 2013 at 05:20 PM.
-
June 6th, 2013, 07:39 PM
#3
Re: Error in implenting class from .h into .cpp
Originally Posted by Zyrion
I've been stressing on finding these errors that my compiler is giving me in this code.
Code:
myPing->Ping;
~ping();
What are you trying to do with that line of code in red?
Regards,
Paul McKenzie
-
June 8th, 2013, 01:46 AM
#4
Re: Error in implenting class from .h into .cpp
Originally Posted by Paul McKenzie
Code:
myPing->Ping;
~ping();
What are you trying to do with that line of code in red?
Regards,
Paul McKenzie
I was being dumb! I forgot destructors are automatically invoked when an object is destroyed or goes out of scope. I should have used the keyword delete myPing, ( I was trying to call the destructor with that line of code, which I see now is wrong.)
-
June 8th, 2013, 06:35 AM
#5
Re: Error in implenting class from .h into .cpp
Code:
Ping(string set = "127.0.0.1")
:host(set)
{
host = set;
repeater = false;
}
You don't need host = set in the above as you are using host(set). Also repeater could be initialised in the same way as host. This would give
Code:
Ping(string set = "127.0.0.1")
:host(set), repeater(false)
{}
For your constructors, you have
Code:
Ping()
...
Ping(string set = "127.0.0.1")
...
Ping(host = "127.0.0.1", bool setRepeat = false)
The first constructor is just a variation of the second, which is a variation of the third - which hasn't got a type for host! - and so aren't needed. So your constructors can be simplifed to
Code:
Ping(std::string set = "127.0.0.1", bool setRepeat = false)
:host(set), repeater(setRepeat)
{}
Also in the header file, you will need to refer to string, cout etc via their full scoped names (ie std:: string and std::cout).
In setRepeater and setHost you use the : form of initialisation. However, this form is only valid within a constructor.
As a class instance ceases to exist after the deconstructor has been called, why are you setting variables in the deconstructor?
In your .cpp file,
This is not a valid statement!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 8th, 2013, 09:02 AM
#6
Re: Error in implenting class from .h into .cpp
Originally Posted by Zyrion
I was being dumb! I forgot destructors are automatically invoked when an object is destroyed or goes out of scope.
OK, so why did you dynamically allocate the object? C++ is not Java, C# or some other language that just happens to have new as a keyword. You do not need to call new to create an object.
Code:
Ping myPing(hostA, false);
Then there is no need for delete, and all of those -> turn into . wherever you're using myPing.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|