Thread: Friend and namespace conflict (I think)

  1. #1
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343

    Friend and namespace conflict (I think)

    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.

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Are you using MSVC 6.0 ?

    If so, download the service packs as there is a bug in the compiler(prior to SP 2 or 3) that prevents external definitions of friend member functions. I think this code will work if you place the definition of the function within the Class itself.

    PS: Don't let "Forky" catch you spelling his name like that
    Last edited by foniks munkee; 11-24-2002 at 07:18 PM.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Are you using MSVC 6.0 ???
    Yes I am. I´ve dl the service pack(ver 5) but still experiating the same problem
    Code:
    itsAge' : cannot access private member declared in class 'Test'
    Hmm, any other suggestions???

    Hehe, "Forky"! I´m really sorry I misspelled your name (twice) Fordy! It will not be repeted!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with friend class declaration in a namespace
    By Angus in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2008, 01:29 PM
  2. compiler doesn't recognize my friend
    By pheres in forum C++ Programming
    Replies: 6
    Last Post: 12-05-2008, 11:10 AM
  3. arithmetic operator friend functions
    By linucksrox in forum C++ Programming
    Replies: 7
    Last Post: 02-06-2006, 11:39 PM
  4. namspace and friend functions
    By Micko in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2003, 07:20 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM