Reputation: 20068
I'm doing a C++ quiz and I need to say whats wrong in the following code:
class Base {
friend class SubClass;
int n;
virtual int getN()
{
return n;
}
};
class SubClass: public Base {
public:
SubClass() {}
SubClass(const SubClass& s) {}
};
int main()
{
SubClass sc;
if(sc.getN() <= 5)
{
int x = sc.getN();
}
return 0;
}
Besides that n is uninitilized and maybe the object should be created through a Base class pointer, what else could be wrong?
When I run it I get this error:
error: 'virtual int Base::getN()' is private
Upvotes: 6
Views: 1878
Reputation: 361462
As by default every member of a class1 is private
, getN
in the base class is declared private
.
Make getN
public as:
class Base {
friend class SubClass;
int n;
public: //<--------------------- you forgot this
virtual int getN()
{
return n;
}
};
1. I mean, a class defined with the keyword class
. Note that class can be defined with the keyword struct
and union
as well, according to the C++ Standard.
EDIT:
If you think because SubClass
is a friend of Base
, so it can access private members of Base
from outside, then that is wrong. friend
means member functions of SubClass
can access private members of Base
class.
However, if you make main()
friend of Base
, then your code would work:
class Base {
friend int main(); //make main() friend of Base
//...
};
Now from main()
, any private members of Base
can be accessed!
See this demo : http://www.ideone.com/UKkCF
Upvotes: 15
Reputation: 141810
Your compiler should give you some clues...
% g++ -Wall -Wextra -Wshadow -Weffc++ test.cpp
test.cpp:1: warning: ‘class Base’ has virtual functions but non-virtual destructor
test.cpp:10: warning: ‘class SubClass’ has virtual functions but non-virtual destructor
test.cpp: In constructor ‘Base::Base()’:
test.cpp:1: warning: ‘Base::n’ should be initialized in the member initialization list
test.cpp: In constructor ‘SubClass::SubClass()’:
test.cpp:12: note: synthesized method ‘Base::Base()’ first required here
test.cpp: In copy constructor ‘SubClass::SubClass(const SubClass&)’:
test.cpp:13: warning: base class ‘class Base’ should be explicitly initialized in the copy constructor
test.cpp: At global scope:
test.cpp:13: warning: unused parameter ‘s’
test.cpp: In function ‘int main()’:
test.cpp:4: error: ‘virtual int Base::getN()’ is private
test.cpp:19: error: within this context
test.cpp:4: error: ‘virtual int Base::getN()’ is private
test.cpp:21: error: within this context
test.cpp:21: warning: unused variable ‘x’
Upvotes: 4
Reputation: 7824
class Base {
friend class SubClass;
int n;
public:
virtual int getN()
{
return n;
}
};
class members are private as default
Upvotes: 2
Reputation: 7490
the function
virtual int getN() { /*...*/ }
is private(by default) within your Base class. Since it's not overloaded within you SubClass, it stays private when you call it within your main function.
If you want to make it legal, make it public within the class:
public:
virtual int getN() { /*...*/ }
Upvotes: 2
Reputation: 46607
The error message says it all: getN
is implicitly private
, which is the default for classes in C++.
Use
class Base {
// ...
public:
virtual int getN();
};
to make it a publicly accessible member. The facts that SubClass
is a friend of Base
and that getN
is called through an instance of Subclass
don't matter here - getN
is simply not accessible from within main()
, where it is called from. You could make main
a friend
of Base
, however.
Upvotes: 3