I have a problem in my private constructor as below:
Lane.hpp:
namespace sim_mob
{
class B
{
friend class A;
private:
B(int){}
};
}
xmll.hpp:
#include"Lane.hpp"
namespace geo
{
class A
{
public:
A()
{
sim_mob::B b(2);
}
};
}
main.cpp:
#include"xmll.hpp"
int main()
{
geo::A a;
return 0;
}
command: $ g++ main.cpp
In file included from main.cpp:2:0:
Lane.hpp: In constructor ‘geo::A::A()’:
Lane.hpp:10:5: error: ‘sim_mob::B::B(int)’ is private
xmll.hpp:9:23: error: within this context
The point is if I didn't have any arguments in the constructor, I wouldn't receive this error. May I Know why i am getting this and how to solve it? many Thanks
In class sim_mob::B
you befriend a class sim_mob:A
but you hope that this friendship extends to geo::A
which it, obviously, does not. To solve this you'd need a declaration of geo::A
before making it a friend:
namespace geo { class A; }
namespace sim_mob
{
class B
{
friend class geo::A;
private:
B(int){}
};
}
The fact that it "works" with the default constructor is, I guess, that you rather declared a function than instantiate an object:
sim_mob::B b();
is a function declaration. If you leave off the parenthesis you should get an error about the default constructor not existing or, if you actually declare it, not being accessible.