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
  • 21,894
  • 86
  • 261
  • 463

2 Answers2

3

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

update

UIHandler a(); 

to

UIHandler a; 
billz
  • 44,644
  • 9
  • 83
  • 100
  • Thanks for the reply, but Duplicate? Where did I open another question like this? Normally when an duplicate, the duplicated thread is produced. Where is it? – PeakGen Jan 24 '13 at 10:37
  • a dup means this question is asked my other person already. :) you can find the answer from previous question – billz Jan 24 '13 at 10:39
  • ohhh!! OK..Luckily I have the answer now anyway, without looking at those ;) – PeakGen Jan 24 '13 at 10:41
  • no problem, welcome to SO – billz Jan 24 '13 at 10:44
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,988
  • 25
  • 42