2017-02-27 84 views
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(); 
    } 
} 

調用構造我想打電話給爲結構類

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

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

    CommunicatorApi::Api::Api(); 

} 

但是我很recieveing錯誤

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

正如錯誤所述,您沒有默認構造函數,因此您不能默認構造類的實例。 –

+0

來自編譯器的錯誤消息非常明確。您沒有類「Api」的默認構造函數,但您正嘗試使用它來構造一個對象。 –

+0

另一個問題:你想做什麼?可能是這樣的:CommunicatorApi :: Api api(); – KonstantinL

回答

2

既然你在形式的自定義的構造函數:

 Api(ApiObserver& observer); 

你可以不使用默認的構造函數,除非你明確地定義它。

您可以使用以下方法之一來解決問題。

選項1:定義一個默認的構造函數

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

然後,你可以使用:

CommunicatorApi::Api::Api(); 

選項2:使用自定義構造函數

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

PS

CommunicatorApi::Api::Api(observer); 

創建臨時對象。你可能想要一個可以稍後使用的對象。爲此,您需要:

CommunicatorApi::Api apiObject(observer); 
+0

我懷疑這個臨時對象不是OP正在尋找的東西。 – KonstantinL

+0

@KonstantinL,我懷疑你是對的。 –