简体   繁体   中英

Call constructor of member object

I try to implement the amySQL, AND I want to intialize this connector intro test function.

class Foo
{
public:
    void Test();

protected:
    amy::connector mgr;

private:
    asio::io_service m;
};

void Foo::Test()
{
    mgr(m);
}

However when i want to compile i get this error:

error: no match for call to '(amy::connector {aka amy::basic_connector<amy::mysql_service>}) (asio::io_service&)'
  mgr(m);

What i do wrong here? Repository to amy sql https://github.com/liancheng/amy

You need to initialize the mgr member inside the Foo class constructor, not in the Test() class method:

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) {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM