jack
jack

Reputation: 91

Can I initialise a value in a parameterised constructor in C++

I have this program in which it is giving error. I am not able to undertsand the reason. PLease elaborate

#include<iostream>
using namespace std;

class room
{
    int length;
    int width;

public:
    room()
    {
        length=0;
        width=0;
    }
    room(int value=8)
    {
        length=width=8;
    }
    void display()
    {
        cout<<length<<' '<<width;
    }
};

main()
{
    room objroom1;
    objroom1.display();
}

The error is call of overloaded function room() is ambiguous.

Upvotes: 0

Views: 137

Answers (4)

Shreevardhan
Shreevardhan

Reputation: 12641

You can correct your class definition to this

class room {
public:
    // Correct way to initialize class members
    room(int value = 0) : length(value), width(value) {}

    // Correct way to display class members
    friend std::ostream & operator <<(std::ostream & out, const room & r) {
        out << r.length << " " << r.width << std::endl;
        return out;
    }
private:
    int length;
    int width;
};

and then use it as

int main() {
    room objroom1;
    std::cout << objroom1;
}

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

The call of constructor in this declaration

room objroom1;

is indeed ambiguous because your class has two default constructors.

According to the C++ Standard (12.1 Constructors)

4 A default constructor for a class X is a constructor of class X that can be called without an argument.

So this constructor

room()
{
    length=0;
    width=0;
}

is default constructor. And this constructor

room(int value=8)
{
    length=width=8;
}

also is default constructor because it can be called without an argument.

Moreover there is a logical inconsistance because when the first constructor is called data members length and width are initialized by zero while when the second constructor is called without an argument the data members are initialized by 8.

And moreover the second constructor even does not use its parameter!:)

Simply define the second constructor the following way

room( int value )
{
    length = width = value;
}

And it would be better to declare the data members as having an unsigned integer type. For example

unsigned int length;
unsigned int width;

Also it is better if function display will have qualifier const. For example

void display() const
{
    cout<<length<<' '<<width;
}

because it does not change class data members.

Upvotes: 3

InvisiblePanda
InvisiblePanda

Reputation: 1609

You have one parameterless constructor and one constructor with a parameter with default value (To clarify this: a default value means that you don't have to specifiy the parameter value, and if you don't, 8 is taken. But calling this constructor without arguments then collides with the default constructor). Thus it cannot be decided which constructor to call when you do

room objroom1;

By the way, I think you want to do

room(int value = 8)
{
    length = width = value; // not = 8
}

But to fix your problem, you could e.g. set the default value in your default constructor (the one without arguments). It looks like that is a design problem on your part, as your room seemingly has 2 "default" values (0 and 8) and you should decide what you actually want to be the default.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409442

You have a default constructor (the room() function), then you have another constructor which takes an int with a default value (the room(int value=8) function). Now, if you create an instance without passing an argument, which constructor should be called? The default constructor or the one with a default int argument?

This is what the message about ambiguous functions is about, that you have two constructors that are equally possible.

Upvotes: 1

Related Questions