How to use structs in class methods - return values and arguments

Hi,

I am struggling to define structs and use them as return values and pass them as arguments to my class methods. I keep getting a string of errors:

TMGPS.cpp:6:1: error: 'UBX_MESSAGE_TYPE' does not name a type
UBX_MESSAGE_TYPE getPacketMessageType (UBX_MSG_CHECKSUM_HEADER pHeader, UBX_MESSAGE_TYPE msgTypes[]){

exit status 1
'UBX_MESSAGE_TYPE' does not name a type

Please if anyone could help? Below is a slimmed down version of my code. I am aware that my return data is pointless, but is just as an example.

tmGPS.h

 class tmGPS {
  
        private:
struct  UBX_MESSAGE_TYPE{
            unsigned char cls;
            unsigned char id;    
            unsigned short length;
            std::string mNemonic;
          } ;

struct UBX_MSG_CHECKSUM_HEADER {
            unsigned char cls;
            unsigned char id;
            unsigned short length;
          };

UBX_MESSAGE_TYPE getPacketMessageType(UBX_MSG_CHECKSUM_HEADER pHeader, UBX_MESSAGE_TYPE msgTypes[]);

public:
}

tmGPS.cpp

#include "tmGPS.h"

tmGPS::tmGPS(){    
    
}
UBX_MESSAGE_TYPE getPacketMessageType (UBX_MSG_CHECKSUM_HEADER pHeader){
UBX_MESSAGE_TYPE msgTypes[1] = {
            {0x01,0x02,0,"NAV-POSLLH"}
          };
return msgTypes[0];

}
tmGPS::UBX_MESSAGE_TYPE 

?

Will your application code (.ino and/or other .cpp files) be calling the getPacketMessageType() function? If so, the UBX_MESSAGE_TYPE type needs to be public.

And, as @anon73444976 pointed out, the type (when used outside of the class definition) will be 'tmGPS::UBX_MESSAGE_TYPE'.

It only be used privately by the class. And not from the .ino file

Fixed your errors so that it at least compiles.
tmGPS.h:

// YOUR REALLY NEED #INCLUDE GUARDS HERE!!!!!
#include <string>

class tmGPS {
  private:
    struct  UBX_MESSAGE_TYPE {
      unsigned char cls;
      unsigned char id;
      unsigned short length;
      std::string mNemonic;
    } ;

    struct UBX_MSG_CHECKSUM_HEADER {
      unsigned char cls;
      unsigned char id;
      unsigned short length;
    };

    UBX_MESSAGE_TYPE getPacketMessageType(UBX_MSG_CHECKSUM_HEADER pHeader);

  public:
    tmGPS() {}
};

tmGPS.cpp

#include "tmGPS.h"

tmGPS::UBX_MESSAGE_TYPE tmGPS::getPacketMessageType (UBX_MSG_CHECKSUM_HEADER pHeader) {
  UBX_MESSAGE_TYPE msgTypes[1] = {
    {0x01, 0x02, 0, "NAV-POSLLH"}
  };
  return msgTypes[0];
}

I'm not sure what will happen when you try to return the value of a local variable of type UBX_MESSAGE_TYPE that contains a std:string member (since it surely has dynamically allocated data). Hopefully the return value will be copy-constructed (or move-constructed) from your local variable 'msgTypes'.

I believe the only problem was the missing scope on the function definition. I don't believe it's needed on the UBX_MESSAGE_TYPE type declarations. The function automatically has the same scope as the struct. This should work:

#include "tmGPS.h"

UBX_MESSAGE_TYPE tmGPS::getPacketMessageType (UBX_MSG_CHECKSUM_HEADER pHeader) {
  UBX_MESSAGE_TYPE msgTypes[1] = {
    {0x01, 0x02, 0, "NAV-POSLLH"}
  };
  return msgTypes[0];
}

Well .... also the function was defined with 2 parameters in the .h file and only 1 parameter in the .cpp file.

Doesn't compile for me. Code in #8 did.

Thank you, thank you, thank you! I tried prefix tmGPS:: to return type and the moved it to the method name, I didn't try add it to both. I did it and all compiles.

tmGPS::UBX_MESSAGE_TYPE tmGPS::getPacketMessageType (...)

Hmmmm... That surprises me... Why?

Thank you all for your kind help and advice. Amazing that you all came to my assistance instantly. Now on with my learning :slight_smile:

Because the type UBX_MESSAGE_TYPE is defined in tmGPS scope.