Study this code snippet
Code:
#include <iostream>
using namespace std;
class Test
{
public:
Test(int age);
~Test();
friend ostream& operator<<(ostream &os, Test &returnobject);
//{//This model works
// return os << returnobject.itsAge;
//}
private:
int itsAge;
};
Test::Test(int age):
itsAge(age)
{}
Test::~Test()
{}
ostream& operator<<(ostream &os, const Test &returnobject)
{
return os << returnobject.itsAge;
}
int main()
{
Test testobject(90);
cout << testobject << endl;
return 0;
}
I get an compiler error on line
Code:
return os << returnobject.itsAge;
The compiler complains that it cannot acces private members of Test! Haven´t I declared it as a friend to operator<<<<(ostream &os, const Test &returnobject) ???
After some searchresult I found this tread http://cboard.cprogramming.com/showt...threadid=29296. After changing Forkys code so it uses namespaces using namespace std; and removed all theese std:: it compiles with the same errors that I get with my code. But when I tried to change my code Forky´s way I´m still stuck with mine original errors. I´m so confused.