[英]error: non-static reference member - can't use default assignment operator
I have a problem where.. Well, I'm not sure.我有一个问题......好吧,我不确定。 I really don't know what it means.
我真的不知道这意味着什么。 I have used this same paradigm illustrated below, where I create an object within a class and give that object a reference to the class.
我使用了下图所示的相同范例,我在 class 中创建了一个 object,并为该 object 提供了对 ZA2F2ED4F8EBC2CBB4B4C2 的引用。 This design is used as an event handler.
此设计用作事件处理程序。
I added a very unrelated piece of code where I loop through a map of objects and re-assign them.我添加了一段非常不相关的代码,在其中循环遍历对象的 map 并重新分配它们。 It has brought up an issue due to the use of the
=
operator.由于使用了
=
运算符,它提出了一个问题。 But I still don't actually know what it's complaining about.但我仍然不知道它在抱怨什么。
// Example program
#include <iostream>
#include <string>
#include <map>
class A {
public:
class Handler {
public:
Handler(A &a):a(a){}
virtual void HandleIt(){
a.DoThings();
}
A &a;
};
A():my_handler(*this){}
Handler my_handler;
void DoThings(){
std::cout << "Im doing things";
}
};
std::map<std::string, A> my_map;
void ReplaceInMap(A &a){
std::map<std::string, A>::iterator it;
for(it = my_map.begin(); it != my_map.end(); ++it){
it->second = a;
}
}
int main()
{
A a;
A b;
A c;
my_map.insert(std::make_pair<std::string, A>("A!", a));
my_map.insert(std::make_pair<std::string, A>("B!", b));
my_map.insert(std::make_pair<std::string, A>("C!", c));
ReplaceInMap(a);
}
C++ 98 C++ 98
In member function 'A::Handler& A::Handler::operator=(const A::Handler&)':
8:15: error: non-static reference member 'A&A::Handler::a', can't use default assignment operator
In member function 'A& A::operator=(const A&)': 6:7:
note: synthesized method 'A::Handler& A::Handler::operator=(const A::Handler&)' first required here
In function 'void ReplaceInMap(A&)': 29:20: note: synthesized method 'A& A::operator=(const A&)' first required here
http://cpp.sh/4chmu http://cpp.sh/4chmu
What does this error mean?这个错误是什么意思? What is the problem?
问题是什么?
What does this error mean?
这个错误是什么意思?
It means what it says literally.这意味着它的字面意思。 The assignment operator of
A
and consequently Handler
cannot be used.不能使用
A
的赋值运算符,因此不能使用Handler
。
Important points:要点:
What is the problem?
问题是什么?
You attempt to assign a non-assignable class.您尝试分配一个不可分配的 class。
Your options are:您的选择是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.