Hey guys I'm playing with structs and trying to get one to include the other:

#include <iostream>
#include <string>

using namespace std;

typedef struct data
{
	string dat;
} DATA;

typedef struct name
{
	string na;
	string age;
	NAME * DATA;


}NAME;

int main()
{

return 0;
}

is this syntax right? it compiles but when i try to access dat through the name struc it wont :S

Cheers,

#include <iostream>
#include <string>

using namespace std;

typedef struct data
{
	string dat;
} DATA;

typedef struct name
{
	string na;
	string age;
//	NAME * DATA;
        DATA* nm;
}NAME;

If your intent is to have an object of type DATA inside the structure named NAME, then do as shown in red above. Avoid making object names the same as structure of class names -- a lot less confusion.

#include <iostream>
#include <string>

using namespace std;

typedef struct data
{
	string dat;
} DATA;

typedef struct name
{
	string na;
	string age;
//	NAME * DATA;
        DATA* nm;
}NAME;

If your intent is to have an object of type DATA inside the structure named NAME, then do as shown in red above. Avoid making object names the same as structure of class names -- a lot less confusion.

so how do I print out DAT? if i do cout << Name->data is all i can do not get access to the dat data?

This article has been dead for over six months. Start a new discussion instead.