Domanda

I'm trying to get some code to compile (This code) but when I comment out line 25:
virtual void info()=0;
it doesn't compile:

shape.cpp: In function ‘int main()’:
shape.cpp:345:11: error: ‘class shape’ has no member named ‘info’
  svec[0]->info();

but keeping line 25 gives a very long error about a pure virtual function...

shape.cpp:77:15: error: cannot declare parameter ‘squ’ to be of abstract type ‘square’
   cube(square squ):
               ^
shape.cpp:30:7: note:   because the following virtual functions are pure within ‘square’:
 class square : public shape {
       ^
shape.cpp:25:16: note:  virtual void shape::info()
   virtual void info()=0;
                ^
shape.cpp:167:20: error: cannot declare parameter ‘rec’ to be of abstract type ‘rectangle’
   cuboid(rectangle rec, double d):
                    ^
shape.cpp:110:7: note:   because the following virtual functions are pure within ‘rectangle’:
 class rectangle : public shape {
       ^
shape.cpp:25:16: note:  virtual void shape::info()
   virtual void info()=0;

and so on...

Can anyone give me an idea about what I'm doing wrong? Thanks.

È stato utile?

Soluzione

The function is declared const in the derived classes, but not in the base class. This means that the derived classes don't override the function; they just declare a different function with the same name.

Either add const in the base class, or remove it in the derived classes.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top