简体   繁体   中英

Class constructor Inside Struct

How can I initialize a class with in a structure with class constructor

Suppose my class can initialize like client c_3(io_service_2);

Now when I initialize same way inside a struct

struct a{
    boost::asio::io_service io_service_2;
    client c_3(io_service_2);
};

I am getting the error like

error: C2061: syntax error : identifier 'io_service_2'

Any help will be appreciated.

If your intent it to default-initialize the io_service_2 member, then use that to initialize c_3 , and both are members, then a member initialization list is needed

struct a
{
    boost::asio::io_service io_service_2;
    client c_3;

    a() : io_service_2(), c_3(io_service_2) {}
};

Note order of member variable declaration in the class is what is important above all else, as that dictates the order of member initialization regardless of the order in the actual member initialization list

Best of luck.

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