简体   繁体   English

即使我重载=运算符,也无法生成赋值运算符

[英]Assignment operator could not be generated even when I overload the = operator

My class is polymorphic and should not be used to be ='d anyways. 我的课程是多态的,不应该被用来做任何事情。 It has a member which is of type Font& and as a result the compiler cannot generate an = operator. 它有一个Font&类型的成员,因此编译器不能生成=运算符。 So I just created dummy implementations of the assignment and copy constructor, put them in the private of the class, but it still warns me about assignment operator not able to get generated. 所以我刚刚创建了赋值和复制构造函数的虚拟实现,将它们放在类的私有中,但它仍然警告我,无法生成赋值运算符。 How else can I get rid of this warning? 我怎么能摆脱这个警告?

Thanks 谢谢

Warning 9 warning C4512: 'AguiWidget' : assignment operator could not be generated c:\\users\\josh\\documents\\visual studio 2008\\projects\\agui\\alleg_5\\agui\\aguiwidget.hpp 250 警告9警告C4512:'AguiWidget':无法生成赋值运算符c:\\ users \\ josh \\ documents \\ visual studio 2008 \\ projects \\ agui \\ alleg_5 \\ agui \\ aguiwidget.hpp 250

The assignment operator that the compiler is warning you about is the one for your own class. 编译器警告您的赋值运算符是您自己的类的赋值运算符。 What you have now is: 你现在拥有的是:

AguiWidget& operator=(const AguiFont &tmp);

What you need is: 你需要的是:

AguiWidget& operator=(const AguiWidget &tmp);

You can disable it. 你可以禁用它。 This won't work, of course, if you're actually trying to use those operators. 当然,如果您真的尝试使用这些运算符,这将不起作用。

Are you sure you got the signatures right? 你确定你有签名吗? Did you make them for each class, base and deriveds? 你是为每个班级,基础和衍生品制作的吗?

Use boost::noncopyable . 使用boost :: noncopyable

class AGUI_CORE_DECLSPEC AguiWidget : private boost::noncopyable

NOTE: 注意:

boost::noncopyable will be enforced for all sub-classes as well. 对于所有子类,也将强制执行boost :: noncopyable。

EDIT: 编辑:

Wow... that's one scary class... 哇......这是一个可怕的课......

Use the pimpl idiom to reduce code interdependencies and improve interface readability. 使用pimpl习语来减少代码相互依赖性并提高界面可读性。

Also you should try to avoid protected member variables (as much as possible) as it breaks encapsulation. 此外,您应该尝试避免受保护的成员变量(尽可能多),因为它会破坏封装。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM