Advertisement

friend functions in MS Visual C++?

Started by February 10, 2001 12:14 AM
1 comment, last by SpikyQube 23 years, 11 months ago
Hey guys, I''m doing a lab for my CS class, I''m using MS Visual C++ 6.0 on Windows 2000, and when i have a friend function, the compiler can''t seem to let the friend function access the private data members of the class that the friend function prototype is in... Anything i can do? Thanks guys -Spiky I want to work for Squaresoft
I want to work for Squaresoft ;)
I just compiled this code with VC++ 6, and everything worked perfectly. You've probably made a mistake in the prototype, so here's my code for comparison:


#include <iostream>using namespace std;class CPrivate{    friend class CFriend;    friend void  Print(const CPrivate& p);public:    CPrivate():m_iPrivate(0)    {}    virtual ~CPrivate()         {}private:    int m_iPrivate;};class CFriend{public:    CFriend()               {}    CFriend(CPrivate& p)    { p.m_iPrivate = 1337; }    virtual ~CFriend()      {}};void Print(const CPrivate& p){    cout << p.m_iPrivate << endl;    return;}int main(int argc, char* argv[]){    CPrivate p;    CFriend f(p);    Print(p);    return 0;}  

It outputs "1337", just like it should. I hope this helps.

Edited by - jonnyfish on February 10, 2001 11:27:40 AM
Advertisement
I got it, I installed Service pack 4 and now it works.

Thanks!

-Spiky

I want to work for Squaresoft
I want to work for Squaresoft ;)

This topic is closed to new replies.

Advertisement