C ++的朋友函数不能访问私有成员 [英] C++ friend function can't access private members

查看:112
本文介绍了C ++的朋友函数不能访问私有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个字符串类有一堆操作符和函数,包括两个朋友函数。这两个会给我带来一些麻烦,因为编译器说他们不能访问私有成员。这是我的string.h:

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 */

这里是主要的。 cpp:

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;
}

这段代码不会编译,编译器声称<$ c $

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

推荐答案

您应该更加注意命名空间。

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;
}

由于不匹配的命名空间,您会收到错误。如果你喜欢使用 MyStr ,那么你应该添加命名空间 MyStr 到重载的好友 。这是你可以做到的:(操作符应该在命名空间 MyStr 中定义)

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;
    }
}

这篇关于C ++的朋友函数不能访问私有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆