c++ - explicitly-defaulted copy assignment operator must return MyClass & -
// task.h class task { public: task() = default; task(const task &) = delete; ~task() = default; task(task &&) = default; const task & operator= (const task &) = default; }; /main.cpp #include <iostream> #include "task.h" int main() { task t; std::cout<<"hello world"<<std::endl; return 0; }
i'm coding c++ on mac os. when compile code above: g++ main.cpp
, error below:
error: explicitly-defaulted copy assignment operator must return 'task &'
i don't understand @ all. operator=
can return non-const reference here? executed same code in windows , worked without error. mac os has special c++ standard?
the problem use = default
.
http://en.cppreference.com/w/cpp/language/copy_assignment
if = default
used, type of return must non-const reference. whereas if code this: const task & operator= (const task &t){}
, works without error.
Comments
Post a Comment