0
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
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
BigL
  • 165
  • 1
  • 2
  • 15
  • 1
    As the error is saying, you don't have a default constructor, so you can't default-construct an instance of the class. – Some programmer dude Feb 27 '17 at 07:10
  • The error message from the compiler is very clear. You don't have a default constructor for the class `Api` and yet you are trying to use it to construct an object. – R Sahu Feb 27 '17 at 07:11
  • Another question: what do you want to do? May be this: CommunicatorApi::Api api(); – KonstantinL Feb 27 '17 at 07:21
  • Possible duplicate of [no default constructor exists for class](http://stackoverflow.com/questions/4981241/no-default-constructor-exists-for-class) – slawekwin Feb 27 '17 at 07:27
  • 2
    Please don't post code with `void main`, as it misleads people and means your code can't be just copied and pasted to try it out. `void main` has never been standard in either C or C++. FTFY this time. – Cheers and hth. - Alf Feb 27 '17 at 07:29

2 Answers2

2

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?

Community
  • 1
  • 1
elanius
  • 487
  • 2
  • 19
2

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);
R Sahu
  • 204,454
  • 14
  • 159
  • 270