"virtual outside class declaration" and "declaration does not declare anything"

K

kelvSYC

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 */
 
J

John Carson

kelvSYC said:
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 */


VC++ 7.1 and Comeau both accept your code without complaint. What compiler
are you using?
 
A

Achintya

kelvSYC said:
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 */

Hi,

The code compiles without errors in VC 6.0

-vs_p...
 
S

shivank

Have you defined Condition::check() anywhere?? because "virtual bool
check() const;" is only a declaration. Similar for the AndCondition
class. If you do not want to define it in Condition then make it pure
by putting: virtual bool check() const = 0;

btw I copied your code to try compiling it. The following code compiled
without any problem on g++ on solaris.

#include<iostream>
#include<vector>

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;
};

bool Condition::check() const
{
return 1;
}
bool AndCondition::check() const
{
return 1;
}

void main(){

return 0;
}


since you have not given the full code, you can find the problem by
matching your code with this.

regards,
shivank
 
A

Alan Johnson

kelvSYC said:
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
 
K

kelvSYC

<428999f9$0$27865$61c65585@un-2park-reader-01.sydney.pipenetworks.com.au>
VC++ 7.1 and Comeau both accept your code without complaint. What compiler
are you using?

gcc 3.3 on Mac OS X 10.4, under the XCode IDE.

Also to be noted that this file is a header file, and is included in
objective-c++ sources (although that shouldn't make any difference).
 
R

Richard Herring

kelvSYC said:
<428999f9$0$27865$61c65585@un-2park-reader-01.sydney.pipenetworks.com.au>


gcc 3.3 on Mac OS X 10.4, under the XCode IDE.

Also to be noted that this file is a header file, and is included in
objective-c++ sources (although that shouldn't make any difference).
Is it possible that any of those sources (or what they include) defines
a macro called 'check' ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top