2017-03-31 24 views
2

我有这个类:无法从“初始清单”转换为UserController的

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

你用C++ 11打开了吗? – NathanOliver

回答

1

您需要来编译此代码。

下面我正在编译没有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$ 
+0

我使用的是Visual Studio 2013,所以我不太确定是否用C++ 11编译我的代码。它适用于一个论点,但我不知道为什么它不适用于两个... – Xyntell

+1

没关系,我想通了。谢谢! – Xyntell

+0

@Xyntell和?它以前如何? –

相关问题