Please explain and help me fix these compiler errors and warnings

Please explain the compiler errors and warnings:

alistair@alistair-Satellite-Pro-L300D:~/C++Practice/AnIntroductionToProgramming$ g++ q2ch11.cpp -std=c++11 -fpermissive -o q2ch11
q2ch11.cpp: In constructor ‘Rectangle::Rectangle(int, int)’:
q2ch11.cpp:15:80: error: no matching function for call to ‘Shape::Shape()’
tangle(int inputLength, int inputWidth):length(inputLength),width(inputWidth){}
^
q2ch11.cpp:6:1: note: candidate: Shape::Shape(int)
Shape(int inputLength):length(inputLength){}
^
q2ch11.cpp:6:1: note: candidate expects 1 argument, 0 provided
q2ch11.cpp:3:7: note: candidate: constexpr Shape::Shape(const Shape&)
class Shape
^
q2ch11.cpp:3:7: note: candidate expects 1 argument, 0 provided
q2ch11.cpp: At global scope:
q2ch11.cpp:25:36: warning: constructors cannot be declared virtual [-fpermissive]
virtual Square(const Square & other):Rectangle(other.length, other.length){}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>

class Shape
{
public:
Shape(int inputLength):length(inputLength){}
virtual ~Shape(){}
protected:
int length;
};

class Rectangle : public Shape
{
public:
Rectangle(int inputLength, int inputWidth):length(inputLength),width(inputWidth){}
~Rectangle(){}
protected:
int length, width;
};

class Square : public Rectangle
{
public:
Square(int length):Rectangle(length,length){}
virtual Square(const Square & other):Rectangle(other.length, other.length){}
~Square(){}
int getLength(){return length;}
protected:
int length;
};

int main()
{
Square Object(5);
std::cout << "The length is << " <<Object.getLength()<<std::endl;
}
Your Rectangle constructor is trying to call the default constructor of Shape, but no such constructor exists. See how you made your Square constructor call a specific constructor of Rectangle? Rectangle must do the same for Shape constructor.

Why does shape have a length variable? What meaning does that have?

Why does Shape even exist?
Last edited on
-fpermissive

Do not do this. Instead, write correct C++.
Topic archived. No new replies allowed.