Access structure values within a class

A little background. My Arduino is receiving data structure over Ethernet via UDP. Upon receiving the structure I am reading the bytes and placing the data into the correct structure and the data prints as expected. My problem is that I want to access this data within multiple classes. I just lack experience with pointers

typedef struct {

  float SampID;
  float freq;
  float CTpvalue;
  float CTpphase;
  float CT1value;
  float CT1phase;
  float CT2value;
  float CT2phase;
  float CT3value;
  float CT3phase;
  float CT4value;
  float CT4phase;
  float VT1value;
  float VT1phase;
  float VT2value;
  float VT2phase;
  float VT3value;
  float VT3phase;
}
SampV;
#include <SPI.h>  
#include <Ethernet2.h>
#include <EthernetUdp2.h>   
#include "Settings.h"
#include "SampV.h"
#include "Overcurrent.h"

EthernetUDP Udp;
Overcurrent test;

SampV *sampIn; //Instance of sampled value structure
char DATA_buf[100]; //Temporary buffer to store recieved samples    
Settings* settingIn; //Instance of settings structure
char set_buf[200]; //Buffer for settings data

byte relayMac[] = {0x90, 0xA2, 0xDA, 0x10, 0x5E, 0xD7};

IPAddress relayIp(169, 254, 255, 155); //IP address of Relay
IPAddress piIp(192, 168, 0, 15); //IP address of Pi

unsigned int relayPort = 8888; //Local port to listen on
unsigned int piPort = 5005; //Destination port

/*Start Ethernet and UDP*/
void Comms_Setup(){
  
    Ethernet.begin(relayMac, relayIp);
    Udp.begin(relayPort);

}

void check_comms()
{
   int packetSize = Udp.parsePacket();
    if(packetSize)
    {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();

    if(Udp.remotePort()==5000){

    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i]);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(DATA_buf,sizeof(DATA_buf));

    sampIn = (SampV*)DATA_buf;
    
  Serial.println("ID: ");
  Serial.println(sampIn->SampID);
    
  Serial.println("Frequency: ");
  Serial.println(sampIn->freq);

  Serial.print("------CTs-------");
  delay(1000);
  Serial.print(sampIn->CTpvalue);
  Serial.print("--");
  delay(100);
  Serial.print(sampIn->CT1value);
  Serial.print("--");
  delay(100);
  Serial.print(sampIn->CT2value);
  Serial.print("--");
  delay(100);
  Serial.print(sampIn->CT3value);
  Serial.print("--");
  delay(100);
  Serial.println(sampIn->CT4value);
  Serial.print("------VTs-------");
  Serial.print(sampIn->VT1value);
  Serial.print("--");
  delay(100);
  Serial.print(sampIn->VT2value);
  Serial.print("--");
  delay(100);
  Serial.println(sampIn->VT3value); 

    test.input_current=sampIn->VT1value;
    }

    else{
         for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i]);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());


    Udp.read(set_buf, sizeof(set_buf));

    settingIn = (Settings*)&set_buf;
    
    Serial.println("Overcurrent active?");
    Serial.println(settingIn->isOvercurrent_active);
    Serial.println("Distance active?");
    Serial.println(settingIn->isDistance_active);
    Serial.println("Differential active?");
    Serial.println(settingIn->isDifferential_active);
    Serial.println("Fault_location");
    Serial.println(settingIn->fault_location/256);
    Serial.println("Fault Severity:");
    Serial.println(settingIn->fault_severity/256);
    Serial.println("Character 1");
    Serial.println(settingIn->characteristic1/256);
    Serial.println("Time Setting1:");
    Serial.println(settingIn->time_setting1);
    Serial.println("plug_setting1");
    Serial.println(settingIn->plug_setting1);
//    Serial.println("Character 2");
//    Serial.println(settingIn->characteristic2);
//    Serial.println("Time Setting2:");
//    Serial.println(settingIn->time_setting2);
//    Serial.println("plug_setting2");
//    Serial.println(settingIn->plug_setting2);
//    Serial.println("Character 3");
//    Serial.println(settingIn->characteristic3);
//    Serial.println("Time Setting3:");
//    Serial.println(settingIn->time_setting3);
//    Serial.println("plug_setting3");
//    Serial.println(settingIn->plug_setting3);
//    Serial.println("k bias");
//    Serial.println(settingIn->K_bias);
//    Serial.println("is current");
//    Serial.println(settingIn->is_current);
;

    
    }
    }
}

For example in a class I would like to access data within the global instance of Samp.

SampV *sampIn; //Instance of sampled value structure

Such as:

#include "Overcurrent.h"

float Overcurrent::calcDelayTime(int PSM, int TMS)
{
  
 Serial.println(sampIn->CT1value);
}

If sampIn (baddddddd variable name, just call it sampleIn) is part of the object (because he, you try the access it via a object methode) make it part of the class.

If it's not a part of the class, why isn't it a parameter for calcDelayTime?

I did attempt to make it a parameter of the class but I couldn't get it to work. For example

#include "Overcurrent.h"
#include "Settings.h"

float Overcurrent::calcDelayTime(const SampV& test, int PSM, int TMS)
{
  
 Serial.println(test->CT1value);
}

but received errors. The structure isn't part of the class because other classes will be using the same data.

That's not how you call a member of a struct ;) test.CT1value is the way, it's not a object.

test.CT1value is the way, it's not a object.

The dot notation is how you access data from an object. The arrow notation is how you access data from an instance. An instance is a pointer to an object.

At least, that's the way I've always understood those terms.

Okay so now my Header and class functions are as follows:

class Overcurrent
{
  public:
        int PSM;
        int input_current;
        int current;
        float calcDelayTime(const Settings&,int PSM, int TMS);       
};
#include "Overcurrent.h"
#include "Settings.h"

float Overcurrent::calcDelayTime(const Settings& test, int PSM, int TMS)
{
  
  Serial.println(test.isDistance_active);
}

I receive the error message:

Arduino: 1.6.7 (Mac OS X), Board: "Arduino/Genuino Uno"

In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: 'Settings' does not name a type
         float calcDelayTime(const Settings&,int PSM, int TMS);       
                                   ^
Overcurrent.h:7: error: ISO C++ forbids declaration of 'parameter' with no type [-fpermissive]
         float calcDelayTime(const Settings&,int PSM, int TMS);       
                                           ^
In file included from sketch/Overcurrent.cpp:2:0:
Settings.h:13: error: 'boolean' does not name a type
   boolean isOvercurrent_active;    //Overcurrent scheme: not_active=0, active=1
   ^
Settings.h:14: error: 'boolean' does not name a type
   boolean isDistance_active;       //Distance scheme: not_active=0, active=1
   ^
Settings.h:15: error: 'boolean' does not name a type
   boolean isDifferential_active;   //Differential scheme: not_active=0, active=1
   ^
Overcurrent.cpp:4: error: prototype for 'float Overcurrent::calcDelayTime(const Settings&, int, int)' does not match any in class 'Overcurrent'
 float Overcurrent::calcDelayTime(const Settings& test, int PSM, int TMS)
       ^
In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: candidate is: float Overcurrent::calcDelayTime(const int&, int, int)
         float calcDelayTime(const Settings&,int PSM, int TMS);       
               ^
exit status 1
'Settings' does not name a type

If I add

const struct &Settings to the parameter then I get this error:

Arduino: 1.6.7 (Mac OS X), Board: "Arduino/Genuino Uno"

In file included from sketch/Overcurrent.cpp:2:0:
Settings.h:13: error: 'boolean' does not name a type
   boolean isOvercurrent_active;    //Overcurrent scheme: not_active=0, active=1
   ^
Settings.h:14: error: 'boolean' does not name a type
   boolean isDistance_active;       //Distance scheme: not_active=0, active=1
   ^
Settings.h:15: error: 'boolean' does not name a type
   boolean isDifferential_active;   //Differential scheme: not_active=0, active=1
   ^
Settings.h:39: error: conflicting declaration 'typedef struct Settings Settings'
 Settings;
 ^
In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: 'struct Settings' has a previous declaration as 'struct Settings'
         float calcDelayTime(const struct Settings&,int PSM, int TMS);       
                                          ^
sketch/Overcurrent.cpp: In member function 'float Overcurrent::calcDelayTime(const Settings&, int, int)':
Overcurrent.cpp:7: error: 'Serial' was not declared in this scope
   Serial.println(test.isDistance_active);
   ^
Overcurrent.cpp:7: error: invalid use of incomplete type 'const struct Settings'
   Serial.println(test.isDistance_active);
                      ^
In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: forward declaration of 'const struct Settings'
         float calcDelayTime(const struct Settings&,int PSM, int TMS);       
                                          ^
exit status 1
'boolean' does not name a type

If I add

Settings isn't a type. struct Settings is. So, that's why a lot of errors were resolved with that change.

Does Settings.h include Arduino.h? It would appear that it doesn't.

PaulS:
Settings isn’t a type. struct Settings is. So, that’s why a lot of errors were resolved with that change.

Does Settings.h include Arduino.h? It would appear that it doesn’t.

Thanks, I included “Arduino.h” in the settings header file and now I get the following error message:

Arduino: 1.6.7 (Mac OS X), Board: "Arduino/Genuino Uno"

In file included from sketch/Overcurrent.cpp:2:0:
Settings.h:40: error: conflicting declaration 'typedef struct Settings Settings'
 Settings;
 ^
In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: 'struct Settings' has a previous declaration as 'struct Settings'
         float calcDelayTime(const struct Settings&,int PSM, int TMS);       
                                          ^
sketch/Overcurrent.cpp: In member function 'float Overcurrent::calcDelayTime(const Settings&, int, int)':
Overcurrent.cpp:7: error: invalid use of incomplete type 'const struct Settings'
   Serial.println(test.isDistance_active);
                      ^
In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: forward declaration of 'const struct Settings'
         float calcDelayTime(const struct Settings&,int PSM, int TMS);       
                                          ^
exit status 1
conflicting declaration 'typedef struct Settings Settings'

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Post ALL of your code, as an attachment, if necessary. I'm not willing to guess what changes you've made to (portions of) code you haven't posted, and, apparently, neither is anyone else.

PaulS:
Post ALL of your code, as an attachment, if necessary. I’m not willing to guess what changes you’ve made to (portions of) code you haven’t posted, and, apparently, neither is anyone else.

Sorry, I just realised I posted a different structure the what I posted originally, apologies. Here is the code:

Overcurrent.cpp

#include "Overcurrent.h"
#include "SampV.h"

float Overcurrent::calcDelayTime(const SampV& test, int PSM, int TMS)
{
  
  Serial.println(test.CTpvalue);
}

Overcurrent.h

class Overcurrent
{
  public:
        int PSM;
        int input_current;
        int current;
        float calcDelayTime(const struct SampV&,int PSM, int TMS);       
};
/*Structure defining the values recieved from the merging unit
 * Recieved from the merging unit is the:
 * - ID
 * - IT peak magnitude
 * - IT phase
 */
typedef struct {

  float SampID;
  float freq;
  float CTpvalue;
  float CTpphase;
  float CT1value;
  float CT1phase;
  float CT2value;
  float CT2phase;
  float CT3value;
  float CT3phase;
  float CT4value;
  float CT4phase;
  float VT1value;
  float VT1phase;
  float VT2value;
  float VT2phase;
  float VT3value;
  float VT3phase;
}
SampV;

Main

#include <SPI.h>  

void setup() {
    
    /*Sets data rate at serial port*/
    Serial.begin(115200); 

    //clock_setup();
    Comms_Setup();  
}

void loop() {

 check_comms();

  delay(1000);
}

So I get the following error message:

Arduino: 1.6.7 (Mac OS X), Board: "Arduino/Genuino Uno"

In file included from sketch/Overcurrent.cpp:2:0:
SampV.h:29: error: conflicting declaration 'typedef struct SampV SampV'
 SampV;
 ^
In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: 'struct SampV' has a previous declaration as 'struct SampV'
         float calcDelayTime(const struct SampV&,int PSM, int TMS);       
                                          ^
sketch/Overcurrent.cpp: In member function 'float Overcurrent::calcDelayTime(const SampV&, int, int)':
Overcurrent.cpp:7: error: 'Serial' was not declared in this scope
   Serial.println(test.CTpvalue);
   ^
Overcurrent.cpp:7: error: invalid use of incomplete type 'const struct SampV'
   Serial.println(test.CTpvalue);
                      ^
In file included from sketch/Overcurrent.cpp:1:0:
Overcurrent.h:7: error: forward declaration of 'const struct SampV'
         float calcDelayTime(const struct SampV&,int PSM, int TMS);       
                                          ^
exit status 1
conflicting declaration 'typedef struct SampV SampV'

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

Once again, you have failed to post ALL of your code. The snippet of OverCurrent.h that you posted hasn't a clue what a SampV is.

I'm done.

PaulS: Once again, you have failed to post ALL of your code. The snippet of OverCurrent.h that you posted hasn't a clue what a SampV is.

I'm done.

I was trying to make it easier for you by only including the relevant files being the class header, .cpp file and the data structure.

PaulS: The snippet of OverCurrent.h that you posted hasn't a clue what a SampV is.

I included the header file containing the data structure SampV in the class .cpp file.

I was trying to make it easier for you by only including the relevant files being the class header, .cpp file and the data structure.

If only the compiler only read the "relevant" portions of the "relevant" files.

If you want help, you'll post what the compiler sees and complains about. Or you'll write your own compiler and start your own forum to discuss your compiler.