0
class Building{
private:
int floor;
public:
Building(int s) { floor = s;}
};

int Main(){
Building twin, star;
Building BlueHouse(5), JangMi(14);
}

I made this code, and when I build the code 'error C2512: 'Building' : no appropriate default constructor available' comes up. It's been only a few weeks since I'v started learning c++ , and I'm having quite a hard time ;< Help me out, c++ masters!

1 Answers1

0

When you write:

Building twin, star;

it means to create twin and star using the default constructor, since you didn't provide any initializers. But you have not defined a default constructor, so this is an error.

To fix this, add a default constructor, e.g. within class Building's public section:

Building(): floor(0) {}

Note that I used the syntax for initializing variables in the constructor, this is actually the same as Building() { floor = 0; } when floor is just an int, but if you had other member variables which were of class type, then there is a difference.

Alternatively you could add a default value to your existing constructor:

Building(int s = 0): floor(s) {}
M.M
  • 138,810
  • 21
  • 208
  • 365