C++友元函数不能访问私有成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15731414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ friend function can't access private members
提问by spinakker
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:
这应该是一个带有一堆运算符和函数的字符串类,包括两个友元函数。而这两个给我带来了一些麻烦,因为编译器说他们无法访问私有成员。这是我的 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:
这是 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.
这段代码不会编译,编译器声称Str
成员是私有的。
回答by 4pie0
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 friendoperators. This is how you can do it: (operators should be defined within namespace MyStr
)
由于不匹配的命名空间,您会收到错误消息。如果您更愿意坚持使用,MyStr
那么您应该MyStr
为重载的友元运算符添加命名空间。这就是你可以做到的:(操作符应该在命名空间中定义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;
}
}
回答by Bo Persson
When you declare the friend functions inside Str
they are considered to be in the immediately enclosing namespace, MyStr
.
当您在内部声明友元函数时,Str
它们被视为位于紧邻的命名空间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.
围绕 .cpp 文件中的运算符。