1

So.. I'm working on a project using C++ and I have multiple structures and I have only one fuction that need to have one of that structures as a parameter.

I already searched everywhere and I only found examples with one structure in specific...

Here is one of my structures

struct {
  int tag_id;
  String serial;
} dataHeader;

And here where I pass it

loraSend(message, &dataHeader);

And here is the function that will receive the struct as a parameter void loraSend(String message, struct MsgStruct &msg_struct)

The problem is that this is just giving this error: "no matching function for call to 'loraSend(String&, <anonymous struct>*)' ".

Can you explain to me how can I fix this?

Thanks a lot

fabian
  • 80,457
  • 12
  • 86
  • 114
ACasal
  • 11
  • 2
  • Is `MsgStruct` a template parameter? – 0x5453 Jun 18 '21 at 16:08
  • MsgStruct is just a struct that I put in the function, and use it to do this "LoRa.write((uint8_t*)(&msg_struct), sizeof(msg_struct));", because I have multiple structures to send in that Write, I want to pass them as a parameter, that's why I put the MsgStruct there – ACasal Jun 18 '21 at 16:14
  • Note that there are no anonymous structs in C++. This is an unnamed struct. – eerorika Jun 18 '21 at 17:35
  • What's the difference between an anonymous struct and an unnamed struct? Those seem like two ways of saying the same thing. – Nathan Pierson Jun 18 '21 at 18:12

3 Answers3

3
using MsgStruct = decltype(dataHeader);


void loraSend(String message, MsgStruct* msg_struct) {
   // ...
}


// elsewhere:
loraSend(message, &dataHeader);
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Thanks for the answer! But, and correct me if I'm wrong, you are saying that the MsgStruct will be the type of dataHeader. Imagine that I have another structure called "dataBody" and I want to pass that dataBody to "loraSend" function. I need to make another ```using MsgStruct = decltype(dataBody);``` ? – ACasal Jun 19 '21 at 10:25
  • @ACasal `MsgStruct` is going to be an alias for type of `dataHeader`. You can only have one alias called `MsgStruct` inside the same scope. – Aykhan Hagverdili Jun 19 '21 at 10:47
  • Ah got it! Thank you !! – ACasal Jun 19 '21 at 19:10
0

You are passing a parameter of type struct dataHeader to a functions that accepts a second parameter of type struct MsgStruct. That is why you are getting an error message.

Your function loraSend could be overloaded (i.e., write another copy of it with a second parameter type of struct MsgStruct) or alternately, your second parameter could be a void pointer (which can point to anything) and loraSend would need to figure out its parameter type.

Logicrat
  • 4,438
  • 16
  • 22
0

Standard C++ does not support anonymous structs, but it does support anonymous unions. So I don't see the reason why you shouldn't do:

struct MsgStruct {
  int tag_id;
  String serial;
} dataHeader;

Instead of typedef or using declarations.


The structure

struct {
  int tag_id;
  String serial;
} dataHeader;

Is an unnamed ([class.pre]) struct:

A class-specifier whose class-head omits the class-head-name defines an unnamed class.

An example of an anonymous struct would be:

struct {
  int a;
  int b;
};

Compiling the above with -pedantic should state that anonymous struct is not iso c++, however some compilers have extensions for it.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • 1
    C++ *does* support anonymous structs. OP's code is legal. – Aykhan Hagverdili Jun 18 '21 at 17:44
  • OP's code shows an unnamed struct and not an anonymous struct; ISO c++ prohibits anonymous structs. – Andreas DM Jun 18 '21 at 18:12
  • Apparently this compiler calls it an anonymous struct. – Aykhan Hagverdili Jun 18 '21 at 18:14
  • Yes it says "anonymous" but I forgot to say and don't know if it is relevant or not, but I'm using Arduino IDE! And actually the DEV C++ calls to it anonymous too ```[Warning] anonymous type with no linkage used to declare variable ' dataHeader' with linkage``` – ACasal Jun 19 '21 at 10:27