0
votes

I'm working with the Arduino with an ethernetshield. I have made a communication class with where i declare a server object (from the ethernet library). Now i want to initialise this object in the class constructor. The constructor of the object takes one parameter (int portnumber).

What is have done is the following:

In the headerfile is declare the server object as a pointer (*Server).

 EthernetServer *ServerObject;

And in the communicator class constructor i initialise the object like this:

  *ServerObject = EthernetServer(5000);

The problem is that the constuctor of the ServerObject Always needs the parameter so i can't just simpely declare it in the headerfile.

Any hints and help is welcome! Thanks in advance!

The header file of the communication class looks like this:

class CommunicationModuleTCPIP : public CommunicationModule
{
public:

CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPServerIPAdress,     int             ServPort,     bool Server);
CommunicationModuleTCPIP();

int Connect();
void Send();
void Recieve();
void SendBuffer(char Buffer[], int Size);
void RecieveBuffer(char Buffer[], int Size);

byte Mac[];
IPAddress ServerIP;
int ServerPort;

EthernetClient Client;
EthernetServer *ServerObject;


    private:
};

And the class constructor looks like this:

    Serial.begin(9600);
delay(1000);
char Buffer[10];

Serial.println("Setting up server");

// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };

*ServerObject = EthernetServer(5000);

//Mac = byte[] { 0x90, 0xA2, 0xDA, 0x0D, 0xA4, 0x13 };   //physical mac address
for (int Index = 0; Index < 6; Index++) {
    Mac[Index] = MacAdress[Index];
};

ServerIP = ServerIPAdress; // IP Adress to our Server
ServerPort = ServPort;

// initialize the ethernet device
Ethernet.begin(Mac, ServerIP, gateway, subnet);

// start listening for clients
this.ServerObject.begin();

Serial.println("Server setup");

// if an incoming client connects, there will be bytes available to read:
Client = ServerObject.available();
if (Client.connected()) {
    Serial.println("client connected");
    // read bytes from the incoming client and write them back
    // to any clients connected to the server:

    RecieveBuffer(Buffer, 10);

    Serial.print(Buffer);
}

Update

After trying Craig's suggestion i get some new compiler errors:

But no i got the following erros:C:\Users\Gebruiker\Documents\Arduino\libraries\CommunicationModule\CommunicationModuleTCPIP.cpp:12: error: no matching function for call to 'EthernetServer::EthernetServer()' C:\Program Files (x86)\Arduino\libraries\Ethernet/EthernetServer.h:14: note: candidates are: EthernetServer::EthernetServer(uint16_t) C:\Program Files (x86)\Arduino\libraries\Ethernet/EthernetServer.h:9: note: EthernetServer::EthernetServer(const EthernetServer&)

Do you have any idea how to solve this? In my understanding it says that there is nog constructor of the Ethernetserver class withouth any parameters. But i'm giving this parameter in the initializerlist by doing the following:

CommunicationModuleTCPIP::  CommunicationModuleTCPIP(byte MacAdress[], IPAddress ServerIPAdress, int ServPort, bool Server)
:ServerObject(5000)
 {
   // Code
 }

Does anybody have an idea?

1
Why is ServerObject a pointer? - Craig
Hi, thanks for the comment! It's a pointer because i can't declare the ServerObject in the headerfile, (i think) because this is of the constructor of the serverObject that needs a parameter (portNumber). Is there a another way? - Roy08
Don't make it a pointer and use an initialization list in your constructor to create it. - Craig
@Craig Thanks for your comment! Can you expain how to create an initialization list? - Roy08

1 Answers

0
votes

Don't declare ServerObject as a pointer and then create the instance using an initializer list.

This is what your constructor will look like:

CommunicationModuleTCPIP::CommunicationModuleTCPIP(/*...*/)
    :ServerObject(5000)
{
   /* code */
}

This page has some information about initializers.

Be aware that C++ on the arduino is limited. In particular you do not have the STL and new and delete are not implemented.