0

I compile the code like so: c++ .\Run.cpp .\Vector.cpp The Vector.cpp and the Vector.h are in the same folder.. I've seen many threads about this problem, but they didn't help, im sure that there is a miniature problem but i can't see it... Thank you for the help!

This is the Vector.h

#ifndef VECTOR_H
#define VECTOR_H
#include <iostream>

template <class T>
class Vector
{
private:
    T *data;
    size_t size;

public:
    Vector() : data(nullptr), size(0) {}
    Vector(const Vector<T> &v);
    ~Vector();
    Vector<T> &operator=(const Vector<T> &v);

    void push_back(T el);
    void push_front(T el);
    void erase(size_t i);
    void clear();

    bool contains(T el);
    T &operator[](size_t i);
    size_t length();
};

template <class T>
std::ostream &operator<<(std::ostream &os, const Vector<T> &v);

#endif

This is the Vector.cpp

#include "Vector.h"
#include <iostream>

template<class T>
size_t Vector<T>::length(){
    return size;
}

template<class T>
T &Vector<T>::operator[] (size_t i){
    return data[i];
}

template<class T>
Vector<T>::Vector(const Vector& v) {
    data = new T[v.size()];
    for (size_t i = 0; i < v.size; i++)
    {
        data[i] = v[i];
    }
    size = v.size();
}

template<class T>
Vector<T> &Vector<T>::operator= (const Vector& v){
    if(this != &v){
        delete data;
        data = new T[v.size];
        for (size_t i = 0; i < v.size; i++)
        {
            data[i] = v[i];
        }
        size = v.size();
    }
    return *this;
}

template<class T>
Vector<T>::~Vector(){
    delete data;
}

template<class T>
void Vector<T>::push_back(T el){
    T* newData = new T[size+1];
    for (size_t i = 0; i < size; i++)
    {
        newData[i] = data[i];
    }
    newData[size] = el;
    size++;
    delete data;
    data = newData;
}

template<class T>
void Vector<T>::push_front(T el){
    T* newData = new T[size+1];
    for (size_t i = 0; i < size; i++)
    {
        newData[i+1] = data[i];
    }
    newData[0] = el;
    size++;
    delete data;
    data = newData;
}

template<class T>
void Vector<T>::erase(size_t _i){
    T* newData = new T[size-1];
    for (size_t i = 0, j = 0; i < size; i++, j++)
    {
        if(i == _i){
            i--;
            continue;
        }
        newData[i] == data[j];
    }
    --size;
    delete data;
    data = newData;
}

The run.cpp

#include "Vector.h"
#include <iostream>
using namespace std;

int main(){
    Vector<int> v;
    v.push_back(4);
    cout << v << endl;
    return 0;
}

The errors: undefined reference to Vector<int>::push_back(int), undefined reference to std::ostream& operator<< <int>(std::ostream&, Vector<int> const&), undefined reference to Vector<int>::~Vector(), undefined reference to `Vector::~Vector()'

Stukata
  • 35
  • 3
  • 2
    Does this answer your question? [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Mikel Rychliski Apr 14 '20 at 13:50
  • You did not implement `operator<<` and the destructor at all and implementations of member functions of class templates should be put in the header file, not a `.cpp` file, so move all of `Vector.cpp` into `Vector.h`. – walnut Apr 14 '20 at 13:50
  • Thank you for the feedback, it did the job... but it was strange for me because when i've made projects before it didn't give me any errors.. – Stukata Apr 14 '20 at 14:00

0 Answers0