gpt4 book ai didi

c++ - 无法从 'initializer-list' 转换为 UserController

转载 作者:太空宇宙 更新时间:2023-11-04 13:03:52 26 4
gpt4 key购买 nike

我有这门课:

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 支持和有(成功)的情况下进行编译。

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$

关于c++ - 无法从 'initializer-list' 转换为 UserController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43146872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com