I am trying to create a class template inheriting from another class template, the first being a class containing nothing but pure virtual methods, and the second one being the one that's going to be manipulated. What I am trying to build is a vector class that has a size determined by the second template argument. The base class will serve to create another vector class whose size will be different for each object of the class.
After fiddling around for 2-3 days, I can't figure out how to fix a series of errors that occur when I try to compile the project.
Here is the header (Vect.hpp)
#ifndef NEWFILE1_HPP
#define NEWFILE1_HPP
#include <cstddef>
#include <iostream>
#include <exception>
#include <stdexcept>
template <class elem>
class Vect
{
public:
virtual elem& operator[] (std::ptrdiff_t nIndex) =0;
virtual const elem& operator[] (std::ptrdiff_t nIndex) const =0;
virtual Vect& operator+() =0;
virtual Vect& operator-() =0;
virtual Vect& operator+(const elem&) =0;
virtual Vect& operator-(const elem&) =0;
//virtual Vect& operator*(const elem&) =0;
virtual ~Vect();
};
template <class elem, std::size_t taille=10>
class Vect_fixe: public Vect<elem>
{
template <typename T, std::size_t D>
friend std::ostream& operator<< (std::ostream&, const Vect_fixe<T, D>&);
public:
Vect_fixe():vecteur(new elem[taille]) {};
virtual elem& operator[] (std::ptrdiff_t nIndex);
virtual const elem& operator[] (std::ptrdiff_t nIndex) const;
virtual Vect_fixe& operator+();
virtual Vect_fixe& operator-();
virtual Vect_fixe& operator+(const elem&);
virtual Vect_fixe& operator-(const elem&);
private:
elem vecteur[taille];
~Vect_fixe(){delete[] vecteur;}
};
and here is the one defining the functions and doing the tests (main.cpp)
#include <cstdlib>
#include "Vect.hpp"
#include <exception>
#include <stdexcept>
template <class elem, std::size_t taille>
elem& Vect_fixe<elem, taille>::operator[] (std::ptrdiff_t nIndex)
{
if (nIndex >= taille)
throw std::out_of_range("Index out of range.");
return vecteur[nIndex];
};
template <class elem, std::size_t taille>
const elem& Vect_fixe<elem, taille>::operator[] (std::ptrdiff_t nIndex) const
{
if (nIndex >= taille)
throw std::out_of_range("Index out of range.");
return vecteur[nIndex];
};
template <class elem, std::size_t taille>
Vect_fixe<elem, taille>& Vect_fixe<elem, taille>::operator +()
{
return *this;
}
template <class elem, std::size_t taille>
Vect_fixe<elem, taille>& Vect_fixe<elem, taille>::operator -()
{
Vect_fixe<elem, taille> temp_v;
for (int i=0; i<taille; i++)
temp_v[i] = -temp_v[i];
return temp_v;
}
template <class elem, std::size_t taille>
Vect_fixe<elem,taille>& Vect_fixe<elem, taille>::operator+(const elem&)
{
Vect_fixe<elem, taille> temp_v;
elem obj;
for (int i=0; i<taille; i++)
temp_v[i] += obj;
return temp_v;
}
template <class elem, std::size_t taille>
Vect_fixe<elem,taille>& Vect_fixe<elem, taille>::operator-(const elem&)
{
Vect_fixe<elem, taille> temp_v;
elem obj;
for (int i=0; i<taille; i++)
temp_v[i] -= obj;
return temp_v;
}
int main() {
int x;
x = 5;
Vect_fixe<int, 10> vect;
//std::cout<< vect << std::endl;
return 0;
}
And the error I get is the following:
g++ -std=c++14 -o dist/Debug/Cygwin_4.x-Windows/cppapplication_2 build/Debug/Cygwin_4.x-Windows/Vect.o build/Debug/Cygwin_4.x-Windows/main.o
build/Debug/Cygwin_4.x-Windows/main.o: In function `Vect_fixe<int, 10ul>::~Vect_fixe()':
/cygdrive/e/University/BA2/Langages de Programmation/Projet C++/CppApplication_2/Vect.hpp:48: undefined reference to `Vect<int>::~Vect()'
/cygdrive/e/University/BA2/Langages de Programmation/Projet C++/CppApplication_2/Vect.hpp:48:(.text$_ZN9Vect_fixeIiLm10EED1Ev[_ZN9Vect_fixeIiLm10EED1Ev]+0x3f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `Vect<int>::~Vect()'
collect2: error: ld returned 1 exit status
What lead to this state was me trying to fix a previous error that was the return of a reference to a local variable. I put the const keyword in some functions, but that didn't seem to fix the problem so I reverted, and this is what happened. The error is really unclear to me, probably because I am used to programming in Python where almost every error is explained.