-
Structs in classes
Here is my code:
Code:
class GK{
public:
struct General {
char* str1;
float i;
};
};
and in my cpp file I have in my main:
Code:
GK* g;
int main()
{
g->General.str1 = "hello world!";
return 0;
}
the problem is:
error C2273: 'function-style cast' : illegal as right side of '->' operator
error C2228: left of '.aspect' must have class/struct/union type
I am using VC++6, can someone tell me whats wrong here?
Thanks
-
You have made a pointer of class type GK. You have not yet made a GK object however, therefore no place to store the string.
-
The pointer to your class is g but you don't have an instance of your class. You also need to alocate some memory and get a pointer to your struct before you can use it.
-
Code:
class GK{
public:
struct General {
char* str1;
float i;
}general; //instance of struct
};
GK* g = new GK; //create a new object of your class
int main()
{
g->general.str1 = "hello world!";
delete g; //free memory used by new
return 0;
}