![](/img/trans.png)
[英]Is there a way to make a class member object's template call a different constructor?
[英]Call constructor of member object
我嘗試實現amySQL,並且想初始化該連接器簡介測試功能。
class Foo
{
public:
void Test();
protected:
amy::connector mgr;
private:
asio::io_service m;
};
void Foo::Test()
{
mgr(m);
}
但是,當我要編譯時,出現此錯誤:
error: no match for call to '(amy::connector {aka amy::basic_connector<amy::mysql_service>}) (asio::io_service&)'
mgr(m);
我在這里做錯了什么? 到amy sql的存儲庫https://github.com/liancheng/amy
您需要在Foo
類構造函數中而不是在Test()
類方法中初始化mgr
成員:
class Foo
{
public:
Foo();
void Test();
private:
asio::io_service m;
protected:
amy::connector mgr;
};
Foo::Foo() : mgr(m) // <-- initialize here!
{
}
void Foo::Test()
{
// use mgr here as needed...
}
您可以在類構造函數的初始化列表中初始化成員
Foo::Foo() : mgr(m) {}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.