I have an .hpp and .cpp file. I want to access the variable in the structure within a class which happens to be in the header .hpp file, in the .cpp file.
In .hpp, I have
class foo{
public:
struct packet{
int x;
u_int y;
};
};
foo(const char*name)
:m_name(name){}
In .cpp I did:
foo *foo_1 = &foo;
printf("The value of x is : %d",foo_1->packet.x);
printf ("The value of y is : %u", foo_1->packet.y);
On doing this I receive the following error:
code_1.cpp:117: error: expected primary-expression before ‘;’ token
code_1.cpp:118: error: invalid use of ‘struct foo::packet’
code_1.cpp:119: error: invalid use of ‘struct foo::packet’
make: *** [code_1] Error 1
My objective is to get the values of x and y in the cpp file. Any suggestion/idea will be really appreciated.
Thanks.
In .hpp you need to declare a variable with the struct type. For example,
inside of your class
In .cpp, try this
You have just defined struct. Try something like this -
struct packet{
int x;
u_int y;
}test;
and in your cpp, access your struct elements like this -
foo_1.test.x
In your class
Foo
, you have defined apacket
struct, but you have not declared any instances of it. What you want is (this is a compileable self-contained example):You declare the struct, but you never put any data in it.