void akcja(int *, int *)
are different from void akcja()
(overloading). You don't define void akcja()
anywhere.
Pure virtual functions and three lever inheritance
Domanda
I have three classes defined in header files:
Organizm.h:
#ifndef Organizm_H
#define Organizm_H
class Organizm {
public:
int sila;
int inicjatywa;
int trup;
virtual void akcja() = 0;
virtual void kolizja() = 0;
virtual void rysowanie() = 0;
};
#endif
Zwierze.h:
#include "config.h"
#include "Organizm.h"
#ifndef Zwierze_H
#define Zwierze_H
class Zwierze : public Organizm {
public:
void akcja(int *, int *);
void kolizja(Organizm *);
};
#endif
And Wilk.h:
#include "Zwierze.h"
#ifndef Wilk_H
#define Wilk_H
class Wilk: public Zwierze {
public:
Wilk();
void rysowanie();
};
#endif
All non virtual methodths are defined in corresponding files: Organizm.cpp, Zwierze.cpp Wilk.cpp
But when compiling I get following error messg:
g++ -c -o main.o main.cpp
main.cpp: In function ‘int main()’:
main.cpp:13:16: error: cannot allocate an object of abstract type ‘Wilk’
w1 = new Wilk();
^
In file included from main.cpp:5:0:
Wilk.h:6:7: note: because the following virtual functions are pure within ‘Wilk’:
class Wilk: public Zwierze {
^
In file included from Swiat.h:2:0,
from main.cpp:3:
Organizm.h:11:16: note: virtual void Organizm::akcja()
virtual void akcja() = 0;
^
Organizm.h:12:16: note: virtual void Organizm::kolizja()
virtual void kolizja() = 0;
^
<wbudowane>: polecenia dla obiektu 'main.o' nie powiodły się
make: *** [main.o] Błąd 1
zsh: exit 2 make
What am I doing wrong and how can I solve that?
Soluzione
Altri suggerimenti
In Zwierze
You are not overriding the methods from Organizm
.
This compiles:
#include <iostream>
using namespace std;
class Organizm {
public:
int sila;
int inicjatywa;
int trup;
virtual void akcja() = 0;
virtual void kolizja() = 0;
virtual void rysowanie() = 0;
};
class Zwierze : public Organizm {
public:
void akcja(){};
void kolizja(){};
};
class Wilk: public Zwierze {
public:
Wilk(){};
void rysowanie(){};
};
int main() {
Wilk wilk;
return 0;
}
In your case already Zwierze
has 2 sets of akcja
and kolizja
declared. One declared AND defined in Organizm
and its implementation. The second and were derived from Organizm
and NOT overridden due to different signatures. What you did was overloading.
you didnt declare void rysowanie() in any of your class, therefore your Wilk class remains a abstract class. and a abstract class cant be instantiated. Moreover, you have overloaded the virtual functions void akcja() and void kolizja(), which is again different and dont make your class an non abstract class.
define the below functions in Organizm virtual void akcja(int *, int *); virtual void kolizja(Organizm *);
and also give body of void rysowanie()