0

I am trying to create a TCP Modbus Master class in c++, and in this class I need to have a Poco::Net::SocketStream object defined.

class TCPModbusMaster {
    private:
        Poco::Net::SocketStream str;

    public:
        TCPModbusMaster(Poco::Net::SocketStream str) {
            this->str = str;
        }
};

But in this situation, visual studio gives me those 2 errors:

1- no default constructor exists for class Poco::Net::SocketStream

2- function "Poco::Net::SocketStream::operator=(const Poco::Net::SocketStream &)" (declared implicitly) cannot be referenced -- it is a deleted function for the line containing (this->str = str)

For error 1, I tried to add a default constructor for the class Poco::Net::SocketStream

Poco::Net::SocketStream::SocketStream() {

}

But I got this error:

no instance of overloaded function "Poco::Net::SocketStream::SocketStream" matches the specified type

What can I do?

X Y
  • 233
  • 1
  • 8
  • Does this answer your question? [Why should I prefer to use member initialization lists?](https://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-lists) – alter_igel Nov 14 '21 at 19:23
  • 2
    TL;DR for the duplicate: members are default constructed before the constructor body runs unless you use a member initialization list. Replace `TCPModbusMaster(SocketStream str) { this->str = str; }` with `TCPModbusMaster(SocketStream str) : str(str) {}` – alter_igel Nov 14 '21 at 19:24
  • Does this answer your question? [no default constructor exists for class](https://stackoverflow.com/questions/4981241/no-default-constructor-exists-for-class) – JaMiT Nov 14 '21 at 20:03
  • *"I tried to add a default constructor for the class"* -- in order to add a member to a class, you have to change the class definition. You cannot arbitrarily add members simply by claiming that your function is a member. – JaMiT Nov 14 '21 at 20:06
  • @alter igel still gives me an error – X Y Nov 14 '21 at 20:22
  • @XY what error? Please considering [edit]ing your question to be up-to-date. Note that attempting to add code to the Poco library is not the right approach, and you should remove the code you added to try and give `SocketStream` a default constructor – alter_igel Nov 15 '21 at 01:08

1 Answers1

1

First you need a StreamSocket (ie. a TCP socket). Then you can create a SocketStream from it.

class TCPModbusMaster {
    private:
        Poco::Net::SocketStream str;

    public:
        TCPModbusMaster(Poco::Net::StreamSocket sock): str(sock){}
};

(Note that streams are not copyable)

Alex
  • 5,159
  • 4
  • 25
  • 33