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

Error in executing the constructor of another class

本文关键字:出错 构造函数 另一个 执行      更新时间:2023-10-16

可能的重复项:
为什么没有调用构造函数?

请看下面的代码

UIHandler.h

#pragma once
class UIHandler
{
public:
    UIHandler();
    ~UIHandler(void);
private:
    //Book *books;
};

汉德勒.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)
    {
    }    

主.cpp

#include <iostream>
#include "UIHandler.h"
using namespace std;
int main()
{
    UIHandler a();
    system("pause");
    return 0;
}

在此代码中,我无法在 UIHandler 中执行构造函数,因为代码运行但没有任何反应。如果我将参数传递给 UIHandler 构造函数,它会按预期工作,即使我不使用该构造函数。为什么?请帮忙!

你声明了一个返回UIHandler类型的函数a,请参阅最烦人的解析

更新

UIHandler a(); 

UIHandler a; 

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

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