請有人可以幫助解釋爲什麼我在OS X上使用Xcode 5.1編譯以下代碼時出現錯誤。Apple LLVM版本5.1(clang-503.0.40)(基於LLVM 3.4svn )。編譯時出錯對構造向量
我想要在下面構造X,並將它傳遞給一個對的向量。
#include <iostream>
#include <string>
#include <vector>
#include <utility>
struct X
{
public:
typedef std::vector<std::pair<std::string, std::string>> VectorType;
X(VectorType& params) : m_params(params)
{
}
VectorType m_params;
};
int main(int argc, const char * argv[])
{
X::VectorType pairs
{
{ "param-1", "some-string-1"}, // pair 0
{ "param-2", "some-string-2"}, // pair 1
{ "param-3", "some-string-3"}, // pair 2
{ "param-4", "some-string-4"}, // pair 3
{ "param-5", "some-string-5"}, // pair 4
{ "param-6", "some-string-6"}, // pair 5
{ "param-7", "some-string-7"} // pair 6
};
X x
{
{pairs[0], pairs[2], pairs[5]}
};
return 0;
}
報告的錯誤是:
/main.cpp:37:7: error: no matching constructor for initialization of 'X'
X x
^
/main.cpp:6:8: note: candidate constructor (the implicit move constructor) not viable: cannot convert initializer list argument to 'X'
struct X
^
/main.cpp:6:8: note: candidate constructor (the implicit copy constructor) not viable: cannot convert initializer list argument to 'const X'
struct X
^
/main.cpp:11:5: note: candidate constructor not viable: cannot convert initializer list argument to 'VectorType &' (aka 'vector<std::pair<std::string, std::string> > &')
X(VectorType& params) : m_params(params)
^
1 error generated.
非常感謝。我不知道。 – ksl 2014-10-01 15:24:16