3

Introduction

I'm really new in C++. I've started read some books and search some stuff in Internet. But my main problem is debug C++ code and the basics.

So, I want create a new class, called ClientTcp. If create the object with no arguments, the IP and the Port shall be standard (127.0.0.1:8000).

I've 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 It seems perfect, but I have a ridiculous error that I can't understand.

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

Line: This error is present in ClientTcp(string, string); line.

Community
  • 1
  • 1
urb
  • 924
  • 1
  • 13
  • 28

2 Answers2

4

In your declaration of ClientTcp(std::string,std::string);, a definition of std::string needs to be available, regardless of whether you include it in some other translation unit.

The fix is to #include<string> in ClientTcp.h.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
3

Try #include<string>, this might be the reason.

Nishant
  • 1,635
  • 1
  • 11
  • 24