0

Possible Duplicate:
Why is there no call to the constructor?

Please have a look at the following code

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;
}

In this code, I am unable to execute the constructor in UIHandler, because the code runs but nothing happens. If I pass a parameter to the UIHandler constructor, it works as it should be, even I take no use of the constructor. Why is that? Please help!

Community
  • 1
  • 1
PeakGen
  • 20,394
  • 79
  • 230
  • 422

2 Answers2

3

You declare a function a which returns UIHandler type, see most vexing parse

update

UIHandler a(); 

to

UIHandler a; 
billz
  • 43,318
  • 8
  • 77
  • 98
1

UIHandler a(); is a function declaration, that will return UIHandler object. Remove () after a

Upd: changed definition to declaration. Thanks to @JesseGood

borisbn
  • 4,872
  • 22
  • 41