0

So im making a program the take the width and height, using 3 classes. The first to initialize the 2 variable, the second to print the value from the third class.

#include<bits/stdc++.h>
using namespace std;
class polygon
{
    protected:
        int width, height;
    public:
        polygon(int a, int b)
        {
            width=a;
            height=b;
        }
}; 
class output
{
    public:
        void print(int a)
        {
            cout<<a<<"\n";
        }
};
class rectangle: public 
polygon,public output
{
    public:
        rectangle (int a, int b)
        {
            width=a;
            height=b;
        }
        int area()
        {
            return width*height;
        }

};
int main()
{
    rectangle rect(5,6);
    rect.print(rect.area());
    return 0;
}

The error message" no matching function for call to polygon:: polygon()"

2 Answers2

4

Because you didn't specify a polygon constructor to call in the rectangle(int, int) constructor, it implicitly calls the default constructor from polygon -- but there is no such constructor, so you get this error. You need to explicitly chain to the appropriate polygon constructor, and then you can also remove the assignments:

rectangle(int a, int b) : polygon(a, b) { }

Note that since this constructor simply passes its arguments straight through to the polygon constructor, you can also opt to just bring all of the constructors from polygon into rectangle by replacing your rectangle constructor with:

using polygon::polygon;
cdhowie
  • 158,093
  • 24
  • 286
  • 300
0
class rectangle: public polygon, public output {
public:
    rectangle (int a, int b) { // Nowhere polygon's constructor declared which
            width = a;         // requires nothing to get constructed
            height = b;
    }
    int area() {
            return width * height;
    }
};

In the class polygon, define the constructor explicitly:

polygon() {}

To remove the conflict.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34