2013-01-24 17 views
0

可能重複:
Why is there no call to the constructor?錯誤執行其他類的構造函數

請看看下面的代碼

UIHandler.h

#pragma once 
class UIHandler 
{ 
public: 
    UIHandler(); 
    ~UIHandler(void); 

private: 
    //Book *books; 
}; 

UIHandler.cpp

#include "UIHandler.h" 
    #include <iostream> 

    using namespace std; 

    UIHandler::UIHandler() 
    {   
     { 
     //action once code goes here 
     }   
     int selection; 

     cout << "..............Welcome to DigitalLab Library..........." << endl << endl;; 
     cout << "Kindly press, " << endl; 
     cout << "1. Enter Books" << endl; 
     cout << "2. Display Books"<< endl; 
     cout << "3. Member Area" << endl; 

     cout << "Your Selection: "; 
     cin >> selection; 
    }   

    UIHandler::~UIHandler(void) 
    { 
    }  

Main.cpp的

#include <iostream> 
#include "UIHandler.h" 

using namespace std; 

int main() 
{ 
    UIHandler a(); 

    system("pause"); 
    return 0; 
} 

在這段代碼中,我無法執行的UIHandler構造函數中,因爲代碼運行,但沒有任何反應。如果我將一個參數傳遞給UIHandler構造函數,它就像它應該那樣工作,即使我不使用構造函數。這是爲什麼?請幫忙!

回答

3

你聲明函數a返回UIHandler類型,請參閱most vexing parse

更新

UIHandler a(); 

UIHandler a; 
+0

感謝您的回覆,但重複?我在哪裏打開這樣的另一個問題?通常當產生重複的副本時,就會產生重複的線程。它在哪裏? –

+0

一個dup意味着這個問題已經問我的其他人。 :)你可以從前面的問題找到答案 – billz

+0

哦!好吧,我現在無論如何都有答案,沒有看那些;) –

1

UIHandler a();是一個函數聲明,表示將返回UIHandler對象。刪除()a

更新:更改定義爲聲明。由於@JesseGood

+2

'declaration'不'definition'。 –

+0

@JesseGood謝謝。我會解決的。好像英語不是我的語言,我總是混淆術語(當然不是意思) – borisbn

+0

感謝您的回覆:)我真的很感激:) –