我有这堂课:
class UserController
{
private:
Repo repo;
Repo adoption;
public:
UserController(const Repo& r, const Repo& a) : repo(r), adoption(a) {}
Dog get(int index) { return this->repo.get(index); };
};
当我尝试创建UserController类型的对象时,如下所示:
UserController controller{ repo1, repo2 };
它给了我错误:"错误C2440:'初始化' :无法转换为初始化列表'到' UserController'"。为什么呢?
答案 0 :(得分:0)
您需要c++11来编译此代码。
下面我编译时没有C ++ 11支持和(成功)。
Georgioss-MacBook-Pro:~ gsamaras$ g++ main.cpp
main.cpp:14:20: error: no matching constructor for initialization of
'UserController'
UserController controller{ repo1, repo2 };
^
main.cpp:2:7: note: candidate constructor (the implicit copy constructor) not
viable: requires 1 argument, but 0 were provided
class UserController
^
main.cpp:8:5: note: candidate constructor not viable: requires 2 arguments, but
0 were provided
UserController(const Repo& r, const Repo& a) : repo(r), adoption(a) {}
^
main.cpp:14:30: error: expected ';' at end of declaration
UserController controller{ repo1, repo2 };
^
;
2 errors generated.
Georgioss-MacBook-Pro:~ gsamaras$ g++ -std=c++0x main.cpp
Georgioss-MacBook-Pro:~ gsamaras$