执行另一个类的构造函数时出错

时间:2013-01-24 07:42:22

标签: c++ visual-studio-2010

  

可能重复:
  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构造函数,它应该工作,即使我不使用构造函数。这是为什么?请帮忙!

2 个答案:

答案 0 :(得分:3)

您声明了一个返回a类型的函数UIHandler,请参阅most vexing parse

更新

UIHandler a(); 

UIHandler a; 

答案 1 :(得分:1)

UIHandler a();是一个函数声明,它将返回UIHandler个对象。在()

之后删除a

更新:将定义更改为声明。感谢@JesseGood