简体   繁体   English

错误:非静态引用成员 - 不能使用默认赋值运算符

[英]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:要点:

  • References are not assignable.参考是不可分配的。
  • The class contains a reference member, and no user defined assignment operator. class 包含一个引用成员,并且没有用户定义的赋值运算符。 Therefore the class is not assignable.因此 class 不可分配。
  • You attempt to assign objects of that class.您尝试分配该 class 的对象。

What is the problem?问题是什么?

You attempt to assign a non-assignable class.您尝试分配一个不可分配的 class。

Your options are:您的选择是:

  • Don't use a reference member.不要使用参考成员。 Use a pointer or a reference wrapper (standard library has one since C++11) both of which are assignable.使用指针或引用包装器(标准库自 C++11 起就有一个),两者都是可赋值的。
  • Don't assign the object.不要分配 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:非静态引用成员,不能使用默认赋值运算符 - Error: non-static reference member, can't use default assignment operator 非静态参考成员&#39;int&Property <int> :: value&#39;,不能使用默认赋值运算符 - non-static reference member ‘int& Property<int>::value’, can’t use default assignment operator '非静态引用成员,不能使用默认赋值运算符' - 'non-static reference member, can't use default assignment operator' 错误:非静态引用成员&#39;std :: ostream&Student :: out&#39;,不能使用默认赋值运算符 - error: non-static reference member 'std::ostream& Student::out', can't use default assignment operator &#39;类型非静态const成员不能使用默认赋值运算符&#39; - 这是什么意思? - 'Type non-static const member can't use default assignment operator' - what does this mean? 非静态 const 成员,不能使用默认赋值运算符 - Non-static const member, can't use default assignment operator QtConcurrent错误:引用非静态成员 - QtConcurrent error: reference to non-static member 具有非静态lambda成员的类不能使用默认模板参数? - Class with non-static lambda member can't use default template paramers? 错误:无效使用非静态成员 - error: invalid use of non-static member 为非静态引用成员类编写`operator =` - writing `operator=` for non-static reference member class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM