Problems with constructors

Introduction

I am really new to C ++. I started reading some books and looking for some things on the internet. But my main problem is to debug C ++ code and basics.

So, I want to create a new class called ClientTcp. If you create an object with no arguments, the IP and port must be standard (127.0.0.1:8000).

I read this question Constructor Overloading in C ++ .

So, I created this code:

ClientTcp.h file.

class ClientTcp{
    public:
        // non arguments, create loopback connection
        ClientTcp();

        ClientTcp(std::string, std::string);

        virtual ~ClientTcp();
    protected:
    private:
        std::string ip_, port_;
};

      

ClientTcp.cpp file

#include "ClientTcp.h"

ClientTcp::ClientTcp(){
    ip_ = "127.0.0.1";
    port_ = "8000";
}

ClientTcp::ClientTcp(std::string ip, std::string port){
    ip_.assign(ip);
    port_.assign(port);
}

ClientTcp::~ClientTcp(){
    //dtor
}

      

Main.cpp file

#include <string>
#include <iostream>
#include "json.hpp"
#include <ClientTcp.h>

std::string cip, cport;
cip = "127.0.0.1";
cport = "9510";
ClientTcp c(cip, cport);

      

Problem This seems perfect, but I have a funny error that I can't figure out.

error: expected ‘)’ before ‘,’ token|

      

Line: This error is present on the line ClientTcp(string, string);

.

+3


source to share


2 answers


The ClientTcp(std::string,std::string);

definition must be available in the ad std::string

, whether you include it in some other translation unit.



The fix is #include<string>

in ClientTcp.h

.

+4


source


Try it #include<string>

, that might be the reason.



+3


source







All Articles