[英]Uniform Initialization Syntax in complex hierarchy construction?
我正在使用GCC 4.4.5。
這是我的問題的再現:
#include <vector>
class Test
{
public:
Test( int a, int b = 42 ) : m_a( a ), m_b( b ) {}
private:
int m_a;
int m_b;
};
typedef std::vector<Test> TestList;
class TestMaster
{
public:
TestMaster( TestList tests = TestList() ) : m_tests( tests ) {}
private:
TestList m_tests;
};
現在,這有效:
int main()
{
TestList test_list = { 15, 22, 38 };
return 0;
}
但這不編譯:
class TestManager : public TestMaster
{
public:
TestManager()
: TestMaster( { { 42, 54, 94 } } ) //?
{}
};
int main()
{
TestManager test_manager;
return 0;
}
或者我可能只是不使用正確的語法? 或者GCC錯了嗎?
錯誤 :
g++ -std=c++0x hello_world.cpp
hello_world.cpp: In constructor \u2018TestManager::TestManager()\u2019:
hello_world.cpp:38: erreur: no matching function for call to \u2018TestMaster::TestMaster(<brace-enclosed initializer list>)\u2019
hello_world.cpp:24: note: candidats sont: TestMaster::TestMaster(TestList)
hello_world.cpp:21: note: TestMaster::TestMaster(const TestMaster&)
我也嘗試了一種更簡單的方法(沒有繼承):
TestMaster test_master = { { 42, 54, 94 } };
同樣的錯誤。
任何的想法? 我不明白為什么語義在這里不起作用......
你有太多的建設水平。 初始化列表只在一個級別上工作,所以你需要告訴它你想要的清單應用到TestList
的參數TestMaster
:
TestMaster test_master(TestList({42,54,94}))
然后在TestManager
的構造函數中相同:
TestManager()
: TestMaster( TestList( { 42, 54, 94 } ) )
{}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.