Hi friends.
Today I'm trying to create a small library to deal with a MAX485 in a simple way and I need to pass a structure on the constructor, like a payload so when I want to send some data all I need is to fill the structure fields and send it.I wrote all the code but for testing purpose when I try to access a field from the structure passed by reference,just for testing purpose I get an error.
RS485.h:13:71: error: forward declaration of ‘struct Payload’
RS485(uint8_t hardwareControlPin,unsigned char slaveID,struct Payload *userPayload);
My class consist in some iVars represented like this:
struct PACKET
{
uint8_t startByte;
uint8_t slaveID ;
struct Payload *userData;
uint8_t crc ;
};
uint8_t transceiverPin;//max485 Control Pin
PACKET frame; //A struct variable that contains a pointer to a struct of type Payload
My constructor I have this:
RS485::RS485(uint8_t hardwareControlPin,unsigned char slaveID,struct Payload *userPayload)
{
transceiverPin = hardwareControlPin;
pinMode(transceiverPin, OUTPUT);//Pin that Control Data Direction
digitalWrite(transceiverPin,LOW);//Set the default state to listen
//Build the payload
frame.startByte = headerByte;
frame.slaveID = slaveID;
frame.userData = userPayload;
frame.crc = '9';//Dummy value for now
//Test the passed struct userPayload
Serial.println(frame.startByte); //This prints ok
Serial.println(frame.userPayload->one);//with this it does not compile
}
My goal is to pass a struct created before void setup() and then pass it by reference using:
#include "RS485.h"
struct Payload
{
uint8_t one;
char name;
bool boolVariable;
int var1;
float var2;
double var3;
};Payload myPayload;
void setup()
{
Serial.begin(9600);
RS485 max485(13,'2',&myPayload);//I pass the address of the myPayload to the constructor
This should pass the address of myPayload and assign it on the constructor to the instance variable frame.userData. .I do this in this line on the constructor:
frame.userData = userPayload;
Then I cant print the values passed not sure why.The fact is I have a structure inside a structure and using the
Serial.println(frame.userPayload->one) does not compile.
If I comment this line it compiles ok.If I uncomment it I get the error:
RS485.cpp: In constructor ‘RS485::RS485(uint8_t, unsigned char, Payload*)’:
RS485.cpp:21:29: error: invalid use of incomplete type ‘struct Payload’
Serial.println(userPayload->one);
^
In file included from RS485.cpp:1:0:
RS485.h:13:71: error: forward declaration of ‘struct Payload’
RS485(uint8_t hardwareControlPin,unsigned char slaveID,struct Payload *userPayload);
Any help