[英]friend class doesn't do well with me?
I am trying to deal with friend class
for the first time. 我想第一次与
friend class
打交道。 I wrote the code below: 我写了下面的代码:
class Kind{
private:
friend class Type;
int x;
public:
Kind(){ x=0; }
void setX(int X) { x =X; }
int getX() { return x; }
};
class Type: public Kind {
public:
friend class Kind;
Type(){ }
Kind root;
root.x=3;
};
The compiler tells me that I can not do root.x=3;
编译器告诉我,我不能做
root.x=3;
, What is the problem?? , 问题是什么??
The problem is your trying to execute a statement in a place where the compiler is expecting member declarations. 问题是您尝试在编译器期望成员声明的位置执行语句。 Try putting it into a method
尝试将其放入方法中
class Type : public Kind {
...
void Example() {
Kind root;
root.x = 3;
}
};
You cannot do the assignment as part of the class declaration. 您不能将作业作为课程声明的一部分。 Do it in a member function instead.
改为在成员函数中执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.