Arduino Forum

Using Arduino => Programming Questions => Topic started by: Jonathanese on Jun 12, 2018, 10:07 pm

Title: Can't call Client.connect() inside class
Post by: Jonathanese on Jun 12, 2018, 10:07 pm
I'm trying to create a class that acts as a wrapper for managing various types of internet connections (Ethernet, WiFi, Cellular).

I got Cellular working, which was the hardest by far. But getting Ethernet is proving difficult as well. (The default Ethernet library uses blocking code which is horrible for my application)

Whenever I try to call connect(ip,port) inside my class, I get a compile-time error that says "invalid use of non-static member function".

I'd imagine it has to do with trying pass in data that can be deleted or otherwise non-static into a function that requires it to stick around. As much coding as I do in C++, I don't actually run into this issue terribly often, so I'm prepared for face-palms.

For reference, here is the class:

Code: [Select]
class CESEthernet : public CESCommSlot
{
public:
CESEthernet();
~CESEthernet();

void taskHandler();
bool isReady();
bool isOnNetwork();
bool sendPayload(String payload);

private:
EthState itsState;
EthernetClient itsClient;


void a_PowerUp();
void a_DHCP();
void a_Connect();
void a_Maintain();

void i_Disconnect();
void i_Poweroff();

StopWatch GP_Timer = StopWatch(false);
uint8_t GP_Counter;

byte mac[6];

};




And here is the method:
Code: [Select]
void CESEthernet::a_Connect()
{
if (GP_Timer.start(itsNetConfig.RemoteTimeout)) //Initializes to RemoteTimeout when the timer initially starts
{
DebugMessage(DM_INFO, F("Web Connect"));
}

if (GP_Timer.passed(itsNetConfig.RemoteTimeout))
{
if (itsClient.connect(itsNetConfig.IPAddr.toCharArray, itsNetConfig.rPort))
{
//Success
DebugMessage(DM_INFO, F("Connected To ") + itsNetConfig.IPAddr + ":" + String(itsNetConfig.rPort));
itsState = ETH_DHCP;
GP_Timer.stop();
}
else
{
//Failure
DebugMessage(DM_ERROR, F("Connection Failed"));
GP_Timer.reset();
}
}
}


Title: Re: Can't call Client.connect() inside class
Post by: holmes4 on Jun 12, 2018, 10:43 pm
google!

::

Mark
Title: Re: Can't call Client.connect() inside class
Post by: Jonathanese on Jun 13, 2018, 04:30 pm
Well yeah. I generally don't post something onto a forum unless I'm having trouble finding relevant information on google.
Title: Re: Can't call Client.connect() inside class
Post by: gfvalvo on Jun 13, 2018, 04:38 pm
Can you post an MCVE (https://stackoverflow.com/help/mcve)?
Title: Re: Can't call Client.connect() inside class
Post by: Jonathanese on Jun 13, 2018, 06:11 pm
OK so in the process, I found the issue.

I replaced "itsClient.connect(itsNetConfig.IPAddr.toCharArray" with a string literal. In this case, "Google.com".


guess I'll just have to figure out how to pass the desired website as a char array using some other method.
Title: Re: Can't call Client.connect() inside class
Post by: holmes4 on Jun 13, 2018, 09:46 pm
what you googled "C++ call static member" or somthing like that! I don't think you did. By the way if you look at my initial reply I gave you the answer.

In other words I told you

A. how to find the answer and

B. What the answer was ::

Mark