2017-03-31 23 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和?它以前如何? –

相關問題