This is a piece of my code, I have more class like MathStudent, ArtStudent, etc. which inherits Student class. When I tried to compile, it says “forbids declaration of `vector’ with no type,” what is the problem here?
thanks
class Student {
public:
typedef vector<Student> Friends; // something wrong here?
virtual unsigned int getId() = 0;
//some more pure virtual functions...
};
One problem with the typedef is that
class Studentis an abstract class, so it cannot be default constructed, which is required for types that vectors can be composed of.Another issue (say you removed the fact that
class Studentis abstract) might be that the class isn’t fully defined. You can, in fact, declare a typedef for avector<>with an incomplete class, but you wouldn’t be able to actually use the typedef until the class was fully defined – except to declare pointers or references to the type.In both a cases you may need to think about the class’s overall design – you may want to have a
vector<Student*>instead so the vector can hold any type of student (using pointers since it can’t hold an actual abstract Student object). As others have mentioned using smart pointers (but notstd::auto_ptr<>) would help with managing the lifetimes of object pointed to by the vector.