Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / I hate friends (those in C++ :)) Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Laze Ristoski

April 18, 2005, 04:59 PM

So far I've been sticking to C, but now I'm forced to dive into C++ (of course I used classes and stuff, but I never made my own). Ok, check out this code:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Dude
  5. {
  6. public:
  7.         Dude() {}
  8.         Dude(int X, int Y):x(X), y(Y) {}
  9.         friend ostream& operator<< (ostream&, Dude&);
  10. private:
  11.         int x, y;
  12. };
  13.  
  14. ostream& operator<< (ostream &os, Dude &dood)
  15. {
  16.         cout << dood.x << ":" << dood.y;
  17.         return os;
  18. }
  19.  
  20. int main()
  21. {
  22.         Dude dude(5,11);
  23.         cout << dude << endl;
  24.         return 0;
  25. }


I receive error(s) saying that operator
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Dude
  5. {
  6. public:
  7.         Dude() {}
  8.         Dude(int X, int Y):x(X), y(Y) {}
  9.         friend ostream& operator<< (ostream &os, Dude &dood)
  10.         {
  11.                 cout << dood.x << ":" << dood.y;
  12.                 return os;
  13.         }
  14.  
  15. private:
  16.         int x, y;
  17. };
  18.  
  19. int main()
  20. {
  21.         Dude dude(5,11);
  22.         cout << dude << endl;
  23.         return 0;
  24. }


The function is inside the class, so there's no problem with private members. And friend is there simply because operators overloaded inside a class can only take one parameter. I find this kind of declaration pretty weird. Friend is ok in prototypes, but here I define the function and I make it a friend, but to whom? It's already in the class. I'm a friend to me. ;)

Also, in the first example, if I make x and y public, I receive an error at cout (in main) saying "operator

 
mentalcalculator

April 18, 2005, 05:35 PM

The code you pasted first compiles just fine in Visual Studio .NET 2002.

 
grelle

April 18, 2005, 05:46 PM

Well, I copied your code right off and I don't get any errors. I'm also using VC6, with the latest service pack.

And I don't get the ambiguous error either.

The only thing I can think of is if you have another operator, similiar to the friend, declared somewhere else. Then the compiler might be confused and call the wrong version...

 
bovicide

April 18, 2005, 06:50 PM

First of all, inside your operator

  1.  
  2. ostream& operator<< (ostream &os, const Dude &dood)
  3. {
  4.   os << dood.x << ":" << dood.y;
  5.   return os;
  6. }
  7.  


Anyway I dont think that is the problem, just a general note. Also its a good idea to make the Dude object you passed in const (just a good programming practice). Other than that the code looks like it should compile. Worked fine for me on g++ and VS.NET

 
Laze Ristoski

April 19, 2005, 02:12 AM

Hi,

Thanks for the replies. I tried mingw, which worked a-ok, but it still fails in VC6 (I ain't got no service packs installed). Pretty weird coz I've been using it for quite some time and never having problems. And there's, of course, no reason for the operator being ambiguous since there's nothing else. The above code is all there is (I was just trying out how this works, since I need it for an assignment at university).

@bovicide: I thought about using os instead of cout, and yes I should be using the reference instead of the object itself. A good programming practice as you say. ;)

 
Ftn

April 19, 2005, 02:53 AM

  1.  
  2. ostream& Dude::operator<< (ostream &os, Dude &dood)
  3. {
  4. }
  5.  

 
LastInquisitor

April 19, 2005, 02:54 AM

Your first snippet fails because you actually defined 2 different operators.

The first one, which you declared friend, is not implemented. It's just a prototype.

The implementation a few lines below HAS NOTHING TO DO with it and implements a completely different operator. It's like a simple global function. And it has of course no rights to access private members of class Dude.

To get it to work you must tell the compiler that this shall be the implementation for the prototype.
Just add Dude:: before the operator keyword to do so.

In your second snippet you directly filled the prototype you left empty earlier. Now this IS not a global function but a class member and by that has access to private stuff.

Hope it's clear. Worked all night and my english gets worse the more coffee I insert.

 
Bramz

April 19, 2005, 04:15 AM

LastInquisitor and Ftn,

"To get it to work you must tell the compiler that this shall be the implementation for the prototype. Just add Dude:: before the operator keyword to do so."

You must add Dude:: if you implement a member function of Dude. A friend of Dude is not a member function of Dude. So you don't add Dude:: ! Any free function can be a friend of Dude, and then some more (even other classes and templates can be friends of Dude. And even member functions of other classes can be friends. But neither of them are part of Dude.) You might check part 11.4 of the standard.

So either way, the original code snippet should compile just fine.
But ... it might be that VC6 without service pack 5 (is that the latest one?) doesn't really know all this. Well, a bit of googling gave me this link:

this man seems to have the same problem (and provides a workaround too):
http://www.codeguru.com/forum/archive/index.php/t-43019.html

this is the article ID Q192539 they referred to in the above link. As you can read, it was fixed in service pack 3 :) :
http://support.microsoft.com/kb/q192539/

and here is service pack 5 (so that you don't have to use the workaround):
http://support.microsoft.com/kb/194022/EN-US/


Greetz,
Bramz

 
This thread contains 8 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.