Reputation: 3
im struggling with syntax here: hopefully this question is v simple, im just miising the point.
specifically, if i nest a class within another class, so for instance
class a
{
a //the constructor
{
b an_instance_of_b // an instance of class b
}
};
class b
{
public:
foo()
{
cout << "foo";
}
};
When i try to access this method within B by doing this:
a an_instance_of_a; //declare an instance of a
an_instance_of_a.an_instance_of_b.foo()
^^ this doesnt seem to work. this is simplified (so might be a typo here somewhere). but i know the classes are being setup fine, its just that i cant access the methods inside them if they are nested. what may i be doing wrong?
many thanks.
Upvotes: 0
Views: 251
Reputation: 64682
I do not see a class nested within another class.
I see class "a" end with a bracket-semicolon, before class "b" is started at all. Hence, no nesting.
EDIT
I've reformatted your code to make clear where the classes begin/end.
You are missing a semi-colon after b an_instance_of_b
The constructor of a
is private by default. I think you want a public
constructor.
EDIT 2
I think this is the code you want:
class b; // forward declare "b" so that it is valid in class "a"
class a
{
public:
a(void) //Constructor is now public.
{
// Empty body. There is now a member variable
}
public:
b a_member_instance_of_b;
};
class b
{
// Note: no constructor.
public:
foo()
{
cout << "foo";
}
};
int main(void)
{
a an_instance_of_a;
an_instance_of_a.a_member_instance_of_b.foo();
return 0;
}
Upvotes: 0
Reputation: 3166
Try posting the exact code that compiles & runs. You can cut out un-necessary functions, but the above code won't even compile so it is very hard to nail down the problem.
However taking a stab at it, an_instance_of_b defaults to protected, doesn't it? Which means you will not be able to access it outside of the class. Also judging by your posted code an_instance_of_b isn't even a class variable, it's a local variable within the constructor. You will need to add a Getter to retrieve the instance. Maybe something like this (I haven't done C++ for a while though):
class ClassA
{
public:
hello() { _ClassB = new ClassB(); }
ClassB GetClassB() { return _ClassB; }
private:
ClassB _ClassB;
}
class ClassB
{
public:
foo() {}
}
void SomeRandomFunction()
{
ClassA classA = new ClassA();
classA.GetClassB().foo();
}
Upvotes: 1
Reputation: 3786
This works for me with g++ on Linux:
#include <iostream>
using namespace std;
class B {
public:
void foo() {
cout << "foo" << endl;
}
};
class A {
public:
B b;
};
int main() {
A a;
a.b.foo();
}
I'm thinking b might just be private in your case.
Upvotes: 1
Reputation: 4360
Your
is not a member of a, but a local variable in the constructor of a (and the constructor declaration is missing the parenthesis).an_instance_of_b
What will happen here is that when you create an instance of a, it creates and immediately destroys an instance of b, then it leaves the constructor for a and the a instance is created.
Upvotes: 3
Reputation: 21591
Try this:
class B; // prototype declaration
class hello {
hello() { //the constructor
B an_instance_of_b; // an instance of class b
}
};
class B {
public:
void foo() {
cout << "foo";
}
};
First, they're not nested in your example. There's also no apparent need to do so, anyway.
Second, there were some function and data declaration errors.
Lastly, you need a prototype of B
so that the hello::an_instance_of_b
declaration can work as a forward reference. Or, just declare B
before hello
.
Upvotes: 1