Accessing an array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct whichMessage {
   const char *toMe;
   const char *toYou;
   const char *toThem;
};
const struct whichMessage theMsg[] {
   { "Hello Me", "Hello You", "Hey People" },
   { "Goodbye Me", "Goodbye You", "Goodbye People" },
   { "Silly Me", "Silly You", "Silly People" }
};

void someFunction()
{
   // This should get "Goodbye You"
   theMsg[1].toYou;
   
   // Instead, I get:
   // Error: expression must be a pointer to a complete object type
}


With the above code, I get: Error: expression must be a pointer to a complete object type


Where did I go wrong? Thanks.
Try removing the struct declaration on line 6. This is because otherwise you would be attempting to create a new struct, whereas in this case you want an array of instances of the struct.
In test.h
1
2
3
4
5
struct whichMessage {
   const char *toMe;
   const char *toYou;
   const char *toThem;
};


In test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "test.h"

extern const struct whichMessage theMsg[] {
   { "Hello Me", "Hello You", "Hey People" },
   { "Goodbye Me", "Goodbye You", "Goodbye People" },
   { "Silly Me", "Silly You", "Silly People" }
};

void someFunction()
{
   // This should get "Goodbye You"
   theMsg[1].toYou;
   
   // This now works
}


By separating into two files as described above, this works now.

Thank you, NT3, for pointing me in the right direction.

Still wish I could keep them in the same file, but no biggie.
Topic archived. No new replies allowed.