CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured