Reputation: 595
I have abstract class Transaction which contains couple of normal properties and methods and one method - Behavior - which I made pure virtual, so the classes which inherits from Transaction can implement it on their own.
My Transaction.h
class Transaction abstract{
protected:
...
public:
...
virtual void behavior() = 0;
};
I think this should be correct. Than I have class Cow, which I would like to inherit from the Transaction.
This is how I tried to do it
class Cow : public Transaction {
public:
Cow(int p, Calendar* cal){
this->priority = p;
this->cal = cal;
}
void Behavior() {
...
do some stuff
...
}
};
The problem is, whenever I try to make an object of class Cow. Either it is in Main.cpp or in Cow::Behavior (which are basicly only two places in code I need to be able to create objects of Cow) I end up with an error below.
error C2259: 'Cow' : cannot instantiate abstract class
This really confuses me, as I'm not trying to instantiate an abstract class, but the class that inherits from it. I'm sure this is basic stuff, but I don't seem to be able to solve this problem on my own.
Btw. this is how I try to create new instance of Cow.
Calendar *calendar = new Calendar();
calendar->push(0, new Cow(1, calendar));
where the method Calendar::Push accepts
void Calendar::push(int t, Transaction *tr)
Any ideas?
Thank You!
Upvotes: 0
Views: 137
Reputation: 310980
First of all C++ has no keyword abstract. It seems you are using C++/CLI. Try the following definitions
ref class Transaction abstract{
protected:
...
public:
...
virtual void behavior() = 0;
};
ref class Cow : public Transaction {
public:
Cow(int p, Calendar* cal){
this->priority = p;
this->cal = cal;
}
void behavior() override {
...
do some stuff
...
}
};
Upvotes: 0
Reputation: 1197
Since you are not overriding the pure virtual function behavior in your class Cow [Behavior is taken as different function] so Cow becomes an abstract class too. And rest are C++ semantics.
Upvotes: 0
Reputation: 254461
You've declared a pure virtual function behavior
in the base class, and an unrelated function Behavior
in the derived class. This doesn't override the pure function since it has a different name. C++ is case-sensitive.
Upvotes: 1
Reputation: 108
C++ is case sensitive. You spelled behavior with lowercase 'b' in your base class and Behavior in the derived one.
Upvotes: 0