inheritance class vector

The error is "expected class-name before '{' token";

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
using namespace std;

class array : public vector
{
        
};

int main()
{
     
                       
     system("pause");   
}
It's kind of considered a no no to inherit from std::vector

If you're actually trying to utilize vector, but need a consolidated object with it, just encapsulate it.
1. The word "vector" does not name a class (that's what the compiler is saying): std::vector is a template. To name a class, provide the required paremeters, e.g. vector<int>

2. vectors are not interfaces, they are concrete implementations. They are not even polymorphic: there is no point in publicly deriving from them.
1.
1
2
3
4
template <class T>
class array : public vector<T>
{
};


2. I don't think it's such a bad idea to inherit from std::vector. You could just "extend" the current implementation. If you use std::vector in the same way every time, why not encapsulate your handing methods to append the additional generic methods you're using to std::vector?

When I learned Qt, I found lots of methods in QString really useful. When I had to use std::string in a newer project, I really missed those methods so I extended std::string to include those methods:

1
2
3
4
5
6
7
bool        contains   (const String& str ) const
bool        findReplace(const String& findTerm, const String& replaceTerm) const
double      toDouble   () const
float       toFloat    () const
String      toUpper    () const
std::string toStdString() const
Vector< String > tokenize( const char delimiter=',' ) const


1
2
3
4
5
6
#ifdef STR_EXTENDED
  #include "MyString.h" // class String : public std::string {};
#else
  #include <string>
  typedef std::string String;
#endif 


The only real pain in extending is re-doing all of those constructors and operators.
Last edited on
thanks 1000000000 to all!!! =)
Topic archived. No new replies allowed.