Friend function cant access data member

This is a discussion on Friend function cant access data member within the C++ Programming forums, part of the General Programming Boards category; I am trying to overload the == operator and have it listed as a friend function. However, the compiler (visual ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    3

    Friend function cant access data member

    I am trying to overload the == operator and have it listed as a friend function. However, the compiler (visual studio6) is complaining that the data member I am trying to access is private.

    Code:
    friend bool operator==(const GamePiece& lhs, const GamePiece& rhs);
    
    inline bool operator==(const GamePiece& lhs, const GamePiece& rhs)
    {
    	return (lhs.side == rhs.side);
    }

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    Ive had someone run this on a linux box and had no problems. Is it possible that there is a compiler problem??

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Theres a known bug with MSVC++ concerning friend functions, do a search through google to find links on how to fix it.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    I tried a quick sample doing the same thing you are trying and didn't have problems and I'm using the Microsoft Visual C/C++ 6 environment. Try comming up with a complete but short sample program that has the problem you are describing and post that example for us to see. Do you know what service pack you have installed for the compiler? Maybe you could try updating to the latest service pack release?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Lets see your actual code and the actual error message.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Heres the bug I was talking about:

    http://support.microsoft.com/?kbid=192539&sd=msdn

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    3
    Ive found it. Thanks for the help. One of the workarounds is to qualify the friend functions to the std namespace such as

    bool std:perator==(const & lhs, const & rhs)
    ostream& std:perator<<(ostream& os, const & ref)

    Thanks for the help guys!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. why I cannot access private data of a friend
    By patiobarbecue in forum C++ Programming
    Replies: 2
    Last Post: 02-08-2009, 05:01 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  4. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  5. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21