简体   繁体   中英

Call constructor for struct class

namespace CommunicatorApi
{
    class ApiObserver;

    class COMM_API_EXPORT Api
    {
        public:
            //! Basic constructor
            Api(ApiObserver& observer);
            //! Destructs the object and frees resources allocated by it
            ~Api();
    }
}

I am trying to call

#include <iostream>  
#include "include/communicator_api.h"  

using namespace std;  
int main()  
{  
    cout << "Hello, world, from Visual C++!" << endl;

    CommunicatorApi::Api::Api();

} 

however i am recieveing the error

CommunicatorApi::Api::Api no approprate default constructor available

You already defined constructor with parameter so default constructor is not generated. Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?

Since you have a custom defined constructor in the form of:

        Api(ApiObserver& observer);

you may not use the default constructor unless you explicitly define it too.

You can use one of the following methods to resolve the problem.

Option 1: Define a default constructor

class COMM_API_EXPORT Api
{
    public:
        //! Default constructor
        Api();
        //! Basic constructor
        Api(ApiObserver& observer);
        //! Destructs the object and frees resources allocated by it
        ~Api();
}

then, you can use:

CommunicatorApi::Api::Api();

Option 2: Use the custom constructor

CommunicatorApi::ApiObserver observer;
CommunicatorApi::Api::Api(observer);

PS

CommunicatorApi::Api::Api(observer);

creates a temporary object. You probably want to have an object that you can use later. For that, you need:

CommunicatorApi::Api apiObject(observer);

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