1

I have a class which includes among its members an object of another class.

The header file looks like this:

class LinxArduinoEthernetListener : public LinxListener
{
   EthernetServer ArduinoTcpServer(uint16_t);

Where EthernetServer is itself a class (defined in the Arduino EthernetServer.h library).

Because I do not know the port at compile time, but the server object should be a member of the listener class, I allow the server object to be initialized in the class constructor, and then attempt to reassign that object later, using the following code (in the corresponding .cpp file):

ArduinoTcpServer = EthernetServer(port);

Where "port" is a uint16_t. As far as I know, this is the correct way to reassign to an object variable a newly constructed instance.

And yet, the compiler gives me the following error:

LinxArduinoEthernetListener.cpp:122: error: invalid use of member function (did you forget the '()' ?)

ArduinoTcpServer = EthernetServer((uint16_t)port);

I think this may be related to the error I get for the immediately subsequent function call:

LinxArduinoEthernetListener.cpp:123: error: '((LinxArduinoEthernetListener*)this)->LinxArduinoEthernetListener::ArduinoTcpServer' does not have class type

ArduinoTcpServer.begin();

But I would say it clearly does have a class type, namely the EthernetServer class, as specified in the header file.

What am I doing wrong here?

Marshall Eubanks
  • 265
  • 2
  • 14
  • typo: change to `EthernetServer ArduinoTcpServer(some_value);` – eyllanesc Jul 13 '18 at 19:13
  • When declaring an object you must put the values, not the types. – eyllanesc Jul 13 '18 at 19:14
  • If you are going to initialize it in the constructor then create a pointer: `EthernetServer *ArduinoTcpServer;` and in the constructor: `ArduinoTcpServer = new EthernetServer(some_port)` – eyllanesc Jul 13 '18 at 19:15
  • I wish I could call for arbitration on your -1. It's a well-formed question, and I assume that you presume I didn't try that. Declaring it as "EthernetServer ArduinoTcpServer(22);" gives another compiler error: expected identifier before numeric constant EthernetServer ArduinoTcpServer(22); error: expected ',' or '...' before numeric constant – Marshall Eubanks Jul 13 '18 at 19:18
  • Also "new" isn't implemented by default in AVR gcc. Therefore I'd rather avoid it. – Marshall Eubanks Jul 13 '18 at 19:21
  • Is it implemented `class` but not `new`?, I find it strange – eyllanesc Jul 13 '18 at 19:27
  • Perhaps you shouldn't answer if you don't know what you're talking about. You definitely shouldn't be handing out -1s when can't even give contextually valid suggestions. – Marshall Eubanks Jul 13 '18 at 19:29
  • I have used arduino and I have implemented several libraries, and I have used many times `new`, so I say that I find it strange what you mention. In addition, `new` is part of all the C ++ standards – eyllanesc Jul 13 '18 at 19:32
  • "new" is defined in the Arduino libraries, but not in AVR gcc. I'd rather avoid using the library since I don't actually need dynamic allocation for its own sake here. https://stackoverflow.com/questions/16274451/operator-new-for-arduino – Marshall Eubanks Jul 13 '18 at 19:37
  • but then you must clearly indicate that you are using AVR gcc, in your question you only point arduino. I'm not a fortune teller, I'm not in your mind. :) – eyllanesc Jul 13 '18 at 19:41

2 Answers2

2

EthernetServer ArduinoTcpServer(uint16_t); declares a member function named ArduinoTcpServer. To declare a member variable, omit the parameter type and parentheses. Also add a constructor to initialize the member variable, e.g. :

class LinxArduinoEthernetListener : public LinxListener
{
    public:
        EthernetServer ArduinoTcpServer;

        LinxArduinoEthernetListener(uint16_t port)
        : ArduinoTcpServer(port)
        {
        }
};
Sid S
  • 6,037
  • 2
  • 18
  • 24
1

So, the issue was the declaration of the server object.

Initially, I had it declared as follows:

EthernetServer ArduinoTcpServer(22);

But I would get an error about "expected identifier before numeric constant" referring to the 22. So I googled it, and found someone suggesting that (in some context I don't remember) specifying only the type was sufficient to call the constructor matching that prototype. Doing so allowed the compiler to continue, so I assumed that was valid. However, it seems not.

The real issue is that the compiler Arduino IDE uses requires braces initialization, e.g.

EthernetServer ArduinoTcpServer{22};

That seems to work.

Marshall Eubanks
  • 265
  • 2
  • 14