Reputation: 45
I'm writing an application where I will have several derived classes accessed through pointers to a base class. I wish for one of these derived classes to contain a vector of pointers it's neighbors (also of the base class type) in the application, like so:
#include <vector>
class BaseClass
{
public:
BaseClass() { }
virtual ~BaseClass() { }
};
class DerivedClass : virtual public BaseClass
{
public:
DerivedClass() : BaseClass() { }
~DerivedClass() { }
private:
vector<BaseClass*> Neighbors;
};
However, get the following compiler error:
example.cpp:16: error: ISO C++ forbids declaration of ‘vector’ with no type
example.cpp:16: error: expected ‘;’ before ‘<’ token
Is this even possible? If it is possible please someone point out my mistake! The compiler should know what type a BaseClass is as it has just been declared, in fact declaring a member of type BaseClass Foo;
works, so I don't understand why the vector cannot recognise BaseClass*.
Cheers!
Upvotes: 3
Views: 669
Reputation: 476
Intended to be a comment on Answer #1: You should avoid "using ..." declarations in header files. Keep them inside implementation files (.cpp). Google's Style Guideline provides some additional information that the original poster and answer-er may find helpful: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Namespaces. I don't endorse the google styles as a whole.
Upvotes: 1
Reputation: 61970
You didn't put either using namespace std;
, using std::vector
, or std::vector<...>...
#include <vector>
using std::vector; //choice 1
using namespace std; //choice 2
class BaseClass
{
public:
BaseClass() { }
virtual ~BaseClass() { }
};
class DerivedClass : virtual public BaseClass
{
public:
DerivedClass() : BaseClass() { }
~DerivedClass() { }
private:
std::vector<BaseClass*> Neighbors; //choice 3
};
Upvotes: 5