shape class polymorphism compiler error

Having issues with program to create a shape area calculator with circle square and rectangle. the uml goes as follows:
Where the UML has shape as the abstract class with public area():double, getName():string,and getDimensions:string, rectangle derived from shape with protected height, and width, and a public rectangle(h:double, w:double), followed by a derived square from rectangle with just a public square(h:double), and finally a circle derived from shape with a private radius, and a public circle(r:double).

https://gist.github.com/anonymous/8bcd9a215931de03d2e7

Have linked my program and it is giving me the following compiler errors:

error: 'qdebug' was not declared in this scope line 15 of main

error: cannot declare variable 'shp' to be of abstract type 'shape' line 22 of main

error: expected primary-expression before ')' token lines 29 -31 of main

(note previously had qstring as a header file yet changed to string since I was getting error qstring was not declared in this scope.)
Last edited on
error: 'qdebug' was not declared in this scope
--> At main.cpp, you might have forgotten to include the header file where qdebug() is defined

error: cannot declare variable 'shp' to be of abstract type 'shape'
--> At main() at line 3, you cannot instantiate an abstract type 'shape'. It must have been
 
shape *shp;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
  shape shp;
 
  rectangle rec(4.1,5.2);
  square squ(5.1);
  circle cir(6.1);
 
  qDebug() << "this program uses hierarchies for shapes";
  showNameAndArea(&rectangle);
  showNameAndArea(&circle);
  showNameAndArea(&square);
  return 0;
 
};


error: expected primary-expression before ')' token
--> Please put the complete error details including the line number so we can have an idea what's wrong. You can also try to include also both, qstring and string.
Last edited on
Thank you for your response. in regards to the qdebug error I did include the qdebug header file it is on line 9 in the link. Also the cannot declare variable shp I did change it from
shape shp; to shape *shp; and it did remove the error yet now it says unused variable shp for line 22 of the code.
as well the expected primary response error is happening on lines 29 - 31 of the code in main.

Sorry for the missing lines information thank you very much for your quick response.
In regards to the qdebug error I did include the qdebug header file it is on line 9 in the link.
--> Don't use #include <dDebug>, use #include <QDebug> instead

Also the cannot declare variable shp I did change it from
shape shp; to shape *shp; and it did remove the error yet now it says unused variable shp for line 22 of the code.
--> If you are not using it, then remove it..as simple as that...you are having errors and warnings on line of code you are not using

as well the expected primary response error is happening on lines 29 - 31 of the code in main.
--> Try to modify main as below:

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
#include <QtCore/QCoreApplication>
#include "shape.h"
#include "circle.h"
#include "rectangle.h"
#include "square.h"
#include <String>
#include <iostream>
#include <cmath>
#include <QDebug>
 
using namespace std;
 
void showNameAndArea (shape * pshp)
{
    qdebug()<<pshp->getName()
           << " " << pshp->getDimensions()
           <<" area= " << pshp->area();
}
 
int main()
{
  rectangle rec(4.1,5.2);
  square squ(5.1);
  circle cir(6.1);
 
  qDebug() << "this program uses hierarchies for shapes";
  showNameAndArea(/*dont use this --> &rectangle */ &rec);
  showNameAndArea(/*dont use this --> &circle */ &squ);
  showNameAndArea(/*dont use this --> &square */ &cir);

  return 0;
 
};


or if that will not work, then use this...
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
#include <QtCore/QCoreApplication>
#include "shape.h"
#include "circle.h"
#include "rectangle.h"
#include "square.h"
#include <String>
#include <iostream>
#include <cmath>
#include <QDebug>
 
using namespace std;
 
void showNameAndArea (shape * pshp)
{
    qdebug()<<pshp->getName()
           << " " << pshp->getDimensions()
           <<" area= " << pshp->area();
}
 
int main()
{
  rectangle *rec = new rectangle(4.1,5.2);
  square *squ = new square(5.1);
  circle *cir = new circle(6.1);
 
  qDebug() << "this program uses hierarchies for shapes";
  showNameAndArea(rec);
  showNameAndArea(squ);
  showNameAndArea(cir);

  return 0;
 
};
Last edited on
thank you very much for all of your help. For the QDebug error I was missing Q string and I had Qdebug mispelled on line 15. After making all my changes I do have a new error coming up for my shape.cpp the error says:

shape.cpp:6: error: definition of implicitly-declared 'constexpr shape::shape()'
shape:: shape() // error is happening on line 6 [shape::shape()]
^

shape.cpp:

#include "shape.h"
#include <QDebug>
#include <QString>
using namespace std;

shape::shape()
{

};
Last edited on
You did not declare a constructor prototype at shape.h and you are implementing the constructor at shape.cppi
Topic archived. No new replies allowed.