Gargamil
Gargamil

Reputation: 159

C++ template-class Objects in vector

I tried to implement a generic Tableview for console-usage.

So i tried to make everything generic

Now, I try to store the Column of all different types in a vector. Naturally this won't work. My next step was, that i tried to build a kind of abstract//interface class, which will be the base-class for the templated columns.

class IColum{
public:
    virtual ~IColum(){};

    virtual void setHeading(string head) =0;
    virtual void setData(vector<double*> data)=0;
    virtual void setData(vector<string *> data)=0;
    virtual void print(int n)=0;
    virtual vector<size_t> sort(bool mode) =0;
    virtual void reorder(vector<size_t> rf)=0;
};

template<typename T>
class Colum : public IColum{
public:
    Colum(){
        cout<<"TEST";
    };

    Colum(string n,vector<T*> data);
    Colum(string n,vector<T*> data, int f);

    void setHeading(string head);
    void setData(vector<T*> data);
    void print(int n);
    vector<size_t> sort(bool mode);
    void reorder(vector<size_t> rf);
    ~Colum(){};
private:
    string name;        
    vector<Cell<T>> rows; //templated cell-class
};

//Implementation of the functions

Later i want to have this:

IColum * colum1 = new Colum<string>();

vector<IColum*> colums;

colums.push_back(colum1);

vector<IColum *> colums;

Please, please can you help me?

Upvotes: 0

Views: 1218

Answers (1)

Gargamil
Gargamil

Reputation: 159

So. I found the solution.

The main problem was, that the interface contained specific methods virtual void setData(vector<double*> data)=0; virtual void setData(vector<string*> data)=0;

In fact to be generic, this was antigeneric.. so i removed this.

Colum<string> * colum1 = new Colum<string>();
Colum<double> * colum2 = new Colum<double>();

vector<IColum*> colums;

colums.push_back(colum1);
colums.push_back(colum2);

Now it works.

Thanks for your time..

Upvotes: 2

Related Questions