-1

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

Alecto Irene Perez
  • 10,321
  • 23
  • 46
Irinel Iovan
  • 835
  • 1
  • 8
  • 12
  • 4
    You don't "initialize constructors" - constructors initialize things. –  Aug 07 '19 at 21:51
  • If it's `boost::asio` you are using and you have a relatively new version of boost, use `io_context` instead of `io_service`. `io_service` is just a typedef for backwards compatibility. – Ted Lyngmo Aug 07 '19 at 22:04
  • Possible duplicate of [C++ Constructor](https://stackoverflow.com/questions/6724626/c-constructor) –  Aug 07 '19 at 22:44

2 Answers2

4

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...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    @TedLyngmo yes, it should – Remy Lebeau Aug 07 '19 at 22:28
  • 1
    Declaring `m` before `mgr` is super subtle but is crucial here, since members are initialized in order of declaration. If `m` is declared after `mgr`, then the initializer `mgr(m)` will use an unitialized object and will almost certainly cause Undefined Behavior. – alter_igel Aug 07 '19 at 22:52
  • @alterigel Amazing answer, you saved me. I was wondering why i'm gettin core crash, that was impossible to find out. It seems that this was the cause, order of declaration was different from order of initialization. xD – Irinel Iovan Aug 10 '19 at 03:31
1

You could initialize the members in your class constructor's initialization list

Foo::Foo() : mgr(m) {}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218