C/OOP/Library question: object having object member

I'd like to have a class with a private variable "client" which is type of "Client". How do I do this?

First at all I declare the member "Client client;" in the declaration:

// ComEthernetIRC.h
#include "WProgram.h"
#include <Ethernet.h>

class ComEthernetIRC {
  
  public:
    ComEthernetIRC();
    void setup(byte* mac, byte* ip, byte* remoteIp);

  private:
    Client client;
};

But when I try to instantiate an object, I get this error: "Files\arduino-0022\libraries\Ardumote\ComEthernetIRC.cpp:9: error: no match for call to '(Client) (byte*&, int)'"

// ComEthernetIRC.cpp
#include "ComEthernetIRC.h"

ComEthernetIRC::ComEthernetIRC() {
}

void ComEthernetIRC::setup(byte mac[6], byte ip[4], byte remoteIp[4]) {
  Ethernet.begin(mac, ip);
  client(remoteIP, 6667);
}

Class constructors can only be called when the instance of that class is created. And as far as I can tell, the Client class provides no other method of assigning its IP and port numbers.

So, you can't define a Client object, and then call it's constructor later to configure it.

Depending on what the constructor does, you can

client = Client(a, b);

That would create a new instance of Client and then assign it to the existing instance client.

The other option would be to have a function in the class Client that can set the variables for you.

So you would have a function call at the offending like like this:

client.setVars(a, b);

Delta_G:
Depending on what the constructor does, you can

client = Client(a, b);

That would create a new instance of Client and then assign it to the existing instance client.

I tried but it also throws an error: "Ardumote\ComEthernetIRC.cpp.o: In function ComEthernetIRC': C:\Programme\arduino-0022\libraries\Ardumote/ComEthernetIRC.cpp:4: undefined reference to Client::Client()'"

void ComEthernetIRC::setup(byte mac[6], byte ip[4], byte remoteIp[4]) {
  Ethernet.begin(mac, ip);
  client = Client(remoteIP, 6667);
}

The other option would be to have a function in the class Client that can set the variables for you.

It's the arduino ethernet libary's Client class and as jraskell already said it doesn't have a setter method :frowning: (btw. is that bad design? should that be refactored?)

Maybe I could pass a client instance to the setup method... I'll try that...

Edit:
This throws the same "undefined reference to `Client::Client()" error.

void ComEthernetIRC::setup(Client x) {
client = x;
}

Will it let you do this in there? Create a new instance and set the one you have equal to it.

There's no copy constructor in the class, so it might default to something that works.

Client temp(remoteIP, 6667);

client = temp;

If it will let you do that, it would have the same effect. temp will go out of scope as soon as you exit the function, but client will be set.

The simplest thing is to use the new operator (code for which you have to supply) and then defer creating the instance of Client until you are ready. This compiles:

#include <Ethernet.h>
#include <SPI.h>

// new
void * operator new (size_t size) { return malloc (size); }
// placement new
void * operator new (size_t size, void * ptr) { return ptr; }
// delete
void operator delete (void * ptr) { free (ptr); }

class ComEthernetIRC {
  
  public:
    ComEthernetIRC();
    void setup(byte* mac, byte* ip, byte* remoteIp);

  private:
    Client * client;
};


ComEthernetIRC::ComEthernetIRC() :client (NULL) {
}

void ComEthernetIRC::setup(byte mac[6], byte ip[4], byte remoteIp[4]) {
  Ethernet.begin(mac, ip);
  client = new Client (remoteIp, 6667);
}

void setup ()
{
byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,177 };
byte server[] = { 173,194,33,104 }; // Google

 ComEthernetIRC foo;

 foo.setup ( mac, ip, server );
}

void loop () {}

[quote author=Nick Gammon link=topic=71559.msg534593#msg534593 date=1315346870]
This compiles:[/quote]

This will take some time for me to learn & understand :slight_smile: however thanks a lot.