-1

I create a vector class and in qt I wrote this code

this is a click action I am trying to add new value in vector with click action

void MainWindow::manuelclick()
 {
     int b=0;
     vector<int> a;
     a.pushBack(10);
     for(int i=0;i<a.size();i++)
     {
         b=a.getarray(i);
         QString str=QString::number(b);
         ui->mbox->setText(str);
     }

these are the errors I got

undefined referance to vector<int>::pushBack(int const&)
undefined referance to vector<int>::size()
undefined referance to vector<int>::vector()
undefined referance to vector<int>::getarray(int)
undefined referance to vector<int>::~vector()
undefined referance to vector<int>::~vector()
collect2: error: ld returned 1 exit status

when I delete this code there is no error

here also my "vector.h"

 #include<iostream>
    template <class T>
    class vector
    {
        int vsize,maxsize;
        T* array;
        void alloc_new();
    public:
        vector();
        vector(int);
        ~vector();
        void pushBack(const T&);
        void show();
        int size();
        T operator[](int);
        T getarray(int);

        vector<T>& operator +=(const vector <T>&);

        template<class TE>
        friend std::ostream& operator<<(std::ostream&,const vector<TE> &);

        template<class TE>
        friend std::istream& operator>>(std::istream&,vector<TE> & );
    };

what should I do thanks for your help

peppe
  • 21,934
  • 4
  • 55
  • 70
cihad
  • 72
  • 3
  • 9

1 Answers1

2

When writing templates, you should define methods right with declaration i.e. in header, not cpp file. Also, I recommend using standard container classes, like QVector or std::vector.

Amartel
  • 4,248
  • 2
  • 15
  • 21
  • What do you mean by separate? Do you have a 'vector.cpp', where you define all vector's methods? – Amartel May 18 '13 at 12:56
  • I have an vector.h and vector.cpp.When I delete the code which is in click event there is no error as I said I think I do something wrong under click event – cihad May 18 '13 at 13:09
  • `I have an vector.h and vector.cpp` and that is your mistake. Move code from cpp to h file. `When I delete the code which is in click event there is no error` that is how templates work in C++. 1. You have to declare them in headers. 2. Specialization of method does not creates, unless you call this method. Read more about templates. – Amartel May 18 '13 at 13:16