C++ friend function can't access private members

╄→尐↘猪︶ㄣ 提交于 2020-01-11 04:35:07

问题


This is supposed to be a string class with a bunch of operators and functions, including two friend functions. And those two cause some trouble for me, because the compiler says that they can not access the private members. Here is my string.h:

#include <iostream>
#ifndef STR_H
#define STR_H

namespace MyStr
{
class Str
{
private:
    unsigned int length;
    char *data;
public:
    Str();
    Str(const Str&);
    Str(const char*);
    Str(char c, unsigned int db);
    ~Str();
    char* cStr() const;
    unsigned int getLength() const;

lots of irrevelant functions here...

    friend int operator/ (const Str&, char);
    friend std::ostream& operator<< (std::ostream&, const Str&);
};
}
#endif /* STR_H */

here is the main.cpp:

#include <iostream>
#include "Str.h"

using namespace std;
using namespace MyStr;

ostream& operator<< (ostream& out,const Str& str)
{
    for (int i=0; i<str.length; i++)
    {
        out<<str.data[i];
    }
    out<<endl;
    return out;
}

int operator/ (const Str& str, char c)
{
    for (int i=0; i<str.length; i++)
    {
        if(str.data[i]==c) return i;
    }
    return -1;
}

This code won't compile, the compiler claiming that the Str members are private.


回答1:


You should pay more attention to namespaces.

class Str {
private:
    unsigned int length;
    char *data;
public:
    Str(){}
    Str(const Str&){}
    Str(const char*){}
    Str(char c, unsigned int db){}
    // maybe something more...
    friend int operator/ (const Str&, char);
    friend std::ostream& operator<< (std::ostream&, const Str&);
};

ostream& operator<< (ostream& out,const Str& str)
{
    for (int i=0; i<str.length; i++)
        out<<str.data[i];
    out<<endl;
    return out;
}

int operator/ (const Str& str, char c)
{
    for (int i=0; i<str.length; i++)
        if(str.data[i]==c) return i;

    return -1;
}

int main()
{
    Str s;
    cout<<s;
    return 0;
}

You get error because of the unmatched namespaces. If you prefer to stick with MyStr then you should add namespace MyStr to overloaded friend operators. This is how you can do it: (operators should be defined within namespace MyStr)

namespace MyStr {  
    ostream& operator<< (ostream& out,const Str& str)
    {
        for (int i=0; i<str.length; i++)
        {
             out<<str.data[i];
         }
        out<<endl;
        return out;
    }

    int operator/ (const Str& str, char c)
    {
        for (int i=0; i<str.length; i++)
        {
            if(str.data[i]==c) return i;
        }
        return -1;
    }
}



回答2:


When you declare the friend functions inside Str they are considered to be in the immediately enclosing namespace, MyStr.

The operators you define are in the global namespace, so the compiler believes that those are two entirely different operators, and not the friends.

You can solve this by adding

namespace MyStr
{

}

around the operators in the .cpp file.



来源:https://stackoverflow.com/questions/15731414/c-friend-function-cant-access-private-members

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!