无法从 'initializer-list' 转换为 UserController
Cannot convert from 'initializer-list' to UserController
我有这个class:
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 };
它给我错误:"error C2440: 'initializing' : cannot convert from 'initializer-list' to 'UserController'"。为什么?
您需要 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$
我有这个class:
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 };
它给我错误:"error C2440: 'initializing' : cannot convert from 'initializer-list' to 'UserController'"。为什么?
您需要 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$