Vector of pointers to base class within a derived class
c++, derived-class, multiple-inheritance, vector
Solution
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
};
Problem
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!