I have struct in class and not know how to call variables from struct, please help ;)
#include <iostream>
using namespace std;
class E
{
public:
struct X
{
int v;
};
};
int main(){
E object;
object.v=10; //not work
return 0;
}
You should define the struct out of the class like this:
If you give the struct no name it will work
Otherwise write X x and write e.x.v
Your
E
class doesn't have a member of typestruct X
, you've just defined a nestedstruct X
in there (i.e. you've defined a new type).Try:
I declared class B inside class A, how do I access it?
Just because you declare your
struct B
insideclass A
does not mean that an instance ofclass A
automatically has the properties ofstruct B
as members, nor does it mean that it automatically has an instance ofstruct B
as a member.There is no true relation between the two classes (
A
andB
), besides scoping.It's not clear what you're actually trying to achieve, but here are two alternatives: