kelvSYC wrote:
This little bit of seeminly innocent code seems to give me these two
errors, all on the line that declares check(). Is there some part of
C++ that I'm missing out on?
class Condition {
public:
Condition() {}
virtual ~Condition() {}
virtual bool check() const;
};
class AndCondition : public Condition {
std::vector<Condition> conditionList;
public:
AndCondition(std::vector<Condition> list =
std::vector<Condition>()) : conditionList(list) {}
virtual ~AndCondition() {}
std::vector<Condition>& getConditionList() { return
conditionList; }
const std::vector<Condition>& getConditionList() const { return
conditionList; }
bool check() const;
};
/* more classes that extend Condition here */
There is nothing syntactically wrong with the code you posted (i.e. I
was able to compile it in a program without the compiler complaining).
However, there are some things that may not be doing what you want. For
your base "Condition" class, do you intend for any and all calls to
"check" to be forwarded to a derived class, or does the base class have
it's own definition somewhere as well? I suspect what you actually want
is for it to be a pure virtual function, denoted:
virtual bool check() const = 0;
Also, you are storing vectors of "Condition" objects. Presumably you
are going to iterate through those vectors and call the "check" member
of each. What you probably intend to be doing here is storing a vector
of objects derived from "Condition", in which case you are going to need
to store pointers, rather than actual objects. That is:
std::vector<Condition *> conditionList;
Further, storing pointers will give the polymorphic behavior you expect
from calling "check". Remember, function calls are only polymorphic if
they are made from a pointer or reference.
Alan