I created a program that uses a friend function to access the private member variabls, it runs perfectly in Shedblood Dev-C++, but when i use Microsoft Visual C++ 6 to compile it, the compiler tell me that "the function is not allowed to access private members." Does this mean that Visual C++ doesn't support friend function?

The following is part of the code:

class String
{
public:
    String ();
    ~String ();
    //member functions
    friend ostream & operator << (ostream &, String &);

private:
    char * m_str;
};

int main ()
{
    //........
}

//implementation of member functions

ostream & operator << (ostream & os, String & str)
{
    os << str.m_str;
    return os;
}

Recommended Answers

All 3 Replies

It means that Visual C++ 6 sucks. This is a known problem with that compiler, and it has nothing to do with your code, which is correct. The newer versions of Visual C++ are much better about such things.

It means that Visual C++ 6 sucks. This is a known problem with that compiler, and it has nothing to do with your code, which is correct. The newer versions of Visual C++ are much better about such things.

you need to put some declares to your program

class String;
ostream& operator << (ostream & os, String & str);

the problem will slove!:twisted:

>you need to put some declares to your program
You need to learn C++ a little before trying to answer a simple question.

>the problem will slove!
No, it won't "slove", as you say. Your suggestion has no relevance to the problem at all. Unless, of course, you want to argue that somehow a forward declaration gives code access to a class' private members.

By the way, don't dig up old threads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.