Accessing an array
Apr 5, 2014 at 10:48pm UTC
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.
Apr 5, 2014 at 10:59pm UTC
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.
Apr 5, 2014 at 11:12pm UTC
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.