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::Byou befriend a classsim_mob:Abut you hope that this friendship extends togeo::Awhich it, obviously, does not. To solve this you’d need a declaration ofgeo::Abefore making it a friend:The fact that it “works” with the default constructor is, I guess, that you rather declared a function than instantiate an object:
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.