Error in implenting class from .h into .cpp
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Error in implenting class from .h into .cpp

  1. #1
    Join Date
    Apr 2013
    Posts
    6

    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.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,634

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    http://www.microsoft.com/visualstudio/eng/downloads

  3. #3
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Error in implenting class from .h into .cpp

    Quote Originally Posted by Zyrion View Post
    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

  4. #4
    Join Date
    Apr 2013
    Posts
    6

    Re: Error in implenting class from .h into .cpp

    Quote Originally Posted by Paul McKenzie View Post
    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.)

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,381

    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,

    Code:
    myPing->Ping;
    This is not a valid statement!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Error in implenting class from .h into .cpp

    Quote Originally Posted by Zyrion View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center