Home > assignment operator > error c4512 assignment operator could not be generated

Error C4512 Assignment Operator Could Not Be Generated

Contents

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and

Assignment Operator Could Not Be Generated Const Member

policies of this site About Us Learn more about Stack Overflow the compiler could not generate operator company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags c4127 Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only

Noncopyable

takes a minute: Sign up Assignment operator could not be generated even when I overload the = operator up vote 5 down vote favorite 2 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. So I just

#pragma Warning

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 c++ visual-studio-2008 warnings share|improve this question edited Nov 24 '10 at 18:20 aschepler 34.3k44696 asked Nov 24 '10 at 18:11 jmasterx 15.7k48195403 1 Post some code please. –casablanca Nov 24 '10 at 18:13 Post some code please. (bis repetitas) –paercebal Nov 24 '10 at 18:14 1 And the exact warning, too. –Leo Davidson Nov 24 '10 at 18:14 Okay, that's a lot of code, can you tell us what line it's actually complaining about? –birryree Nov 24 '10 at 18:15 You shouldn't have removed the entire code, just post the relevant sections. :) –casablanca Nov 24 '10 at 18:20 add a comment| 3 Answers 3 active oldest votes up vote 9 down vote accepted The assignment o

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about boost::noncopyable Stack Overflow the company Business Learn more about hiring developers or posting ads with

C++ Assignment Operator

us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Strange C4512 warning. Why? up vote 7 down vote favorite 1 Take the following bit http://stackoverflow.com/questions/4270118/assignment-operator-could-not-be-generated-even-when-i-overload-the-operator of code that uses boost::asio. #include using boost::asio::ip::tcp; class SocketTest { private: boost::asio::io_service& mIOService; tcp::acceptor mAcceptor; // Comment this line public: SocketTest(boost::asio::io_service& io_service) : mIOService(io_service) , mAcceptor(io_service, tcp::endpoint(tcp::v4(), 8080)) // Comment this line { } }; If you comment the two tagged lines the compiler (Visual Studio 2010) gives out the following warning when compiling on /W4. warning C4512: 'SocketTest' : assignment operator could not be generated What makes http://stackoverflow.com/questions/13719461/strange-c4512-warning-why those two lines so special? Why does their existence allow the generation of the assignment operator? c++ boost warnings boost-asio share|improve this question edited Feb 4 '13 at 20:19 Sam Miller 18.1k34373 asked Dec 5 '12 at 8:59 UberMongoose 8218 add a comment| 1 Answer 1 active oldest votes up vote 6 down vote accepted The reason for this behaviour is that tcp::acceptor is not copyable (it inherits from basic_io_object, whose copy assignment operator is private, so its own copy assignment operator is not implicitly declared). Having a non-copyable member avoids the generation of the implicitly declared copy assignment operator, but does not raise a warning because this is considered the expected behaviour. On the other hand, if you comment-out those two lines, your class is left with just a reference member; this also makes your class non-copyiable, but does raise a warning according to Visual Studio documentation. I agree that this is also expected, but the compiler implementers decided to give you a warning just in case. The reason may be that making a class explicitly non-copyable (such as is the case with basic_io_object) is an explicit design decision, but I am just guessing here. share|improve this answer answered Dec 5 '12 at 9:23 Gorpik 8,26712042 Ah

Now, cheerp. http://leaningtech.com/cheerp/ C++ Assignment operator could not be generated 2014/04/09 Brian Fitzgerald Leave a comment What does this warning mean, and how do you fix it? warning http://blog.bfitz.us/?cat=11 C4512: '' : assignment operator could not be generated The compiler will auto-generate some class members for you default constructor (if no other constructor is explicitly declared) destructor copy constructor (if no move constructor or move assignment operator is explicitly declared) copy assignment operator (if no move constructor or move assignment operator is explicitly declared) C++ 11 added two new auto-generated class members assignment operator (and it added "if destructor then copy constructor and copy assignment operator generation is deprecated"): move constructor (if no copy constructor, move assignment operator or destructor is explicitly declared) move assignment operator (if no copy constructor, copy assignment operator or destructor is explicitly declared) Compiler-generated functions are public and non-virtual. As a reminder, here are the signatures of all of these functions: class Object { assignment operator could Object(); // default constructor Object(const Object& other); // copy constructor Object(Object&& other); // move constructor Object& operator=(const Object& other); // copy assignment operator Object& operator=(Object&& other); // move assignment operator ~Object(); // destructor }; So, what if you can't actually create a meaningful copy assignment operator? For example, if you have const data, you can't assign to it. Remember that the auto-generated copy assignment operator just generates assignment operator code for each member of the class, recursively, and you can't assign to const int, you can only construct it. struct ConstantOne { ConstantOne() : value(1) {} const int value; }; int main(int /*argc*/, char ** /*argv*/) { ConstantOne b; return 0; } This will give you a warning when you compile, because the auto-generated assignment operator is technically illegal, and so the compiler won't generate it. It's a warning, because your code probably doesn't need an assignment operator. For Visual C++, you'll see something like this: warning C4512: 'ConstantOne' : assignment operator could not be generated You have several options. The easiest is just to declare an assignment operator without a body. As long as you never actually try to use

 

Related content

error overload resolution selected implicitly-deleted copy assignment operator

Error Overload Resolution Selected Implicitly-deleted Copy Assignment Operator table id toc tbody tr td div id toctitle Contents div ul li a href Assignment Operator Overloading In C Using Friend Function a li li a href Assignment Operator Vs Copy Constructor a li ul td tr tbody table p library Strings library Containers library Algorithms library Iterators library Numerics library Input output library Localizations library Regular expressions library C Atomic operations library C relatedl Thread support library C Filesystem library C Technical Specifications move assignment operator edit C language Classes General overview class struct types union types Members data c