I have read this thread here:
"Cannot allocate an object of abstract type" error
But I think it does not answer my case...
I have the files:
base.h
#ifndef BASE_H
#define BASE_H
#include <iostream>
using namespace std;
class Base {
public:
Base(){
protected_member = 0;
}
Base(int pm_val): protected_member(pm_val) {cout << "created Base." << endl;}
virtual ~Base(){cout << "deleted Base." << endl;}
virtual int access_Base_pm() = 0;
protected:
int protected_member;
};
#endif
base.cpp (redudant I guess)
#include "base.h"
#include "derived.h"
derived.h
#ifndef DERIVED_H
#define DERIVED_H
#include <iostream>
#include "base.h"
using namespace std;
class Base;
class Derived: public Base {
public:
Derived(){cout << "created Derived." << endl;}
~Derived(){cout << "deleted Derived." << endl;}
int access_Base_pm();
};
#endif
derived.cpp
#include "derived.h"
#include "base.h"
int Derived::access_Base_pm(){
return protected_member;
}
When I run
main_1.cpp
#include <iostream>
#include "base.h"
#include "derived.h"
using namespace std;
int main(){
Base* base;
base = new Derived();
cout << base->access_Base_pm() << endl;
}
everything seems to be fine.
But when I run
main_2.cpp
#include <iostream>
#include "base.h"
#include "derived.h"
using namespace std;
int main(){
Base* base = new Base;
base = new Derived();
cout << base->access_Base_pm() << endl;
}
or
main_3.cpp
#include <iostream>
#include "base.h"
#include "derived.h"
using namespace std;
int main(){
Base(5)* base;
base = new Derived();
cout << base->access_Base_pm() << endl;
}
I get "error: cannot allocate an object of abstract type ‘Base’"
Why? I don't get it. As it says in the other thread, I am accessing the object only via a pointer... what am I missing?