redefinition of template<class T> in C++

Vincent Giuliana picture Vincent Giuliana · Oct 23, 2012 · Viewed 12.2k times · Source

I have searched and searched for a solution to my problem but I cannot seem to find one. I am using Code::Blocks and I am getting a redefinition error of a template class.

Here is my "vectorAux.h" file:

#ifndef vectoraux_h
#define vectoraux_h

#include <vector>
#include <algorithm>
#include <iostream>

template <typename T>
void removeDup(std::vector<T> & v);

template <typename T>
unsigned seqVectSearch(const std::vector<T> & v, unsigned first,
               unsigned last, const T& target);

template <typename T>
void writeVector(const std::vector<T> & v);

#include "vectorAux.cpp"
#endif

and here is my "vectorAux.cpp" file:

#include "vectorAux.h"

#include <vector>
#include <algorithm>
#include <iostream>

template <typename T>
void removeDup(std::vector<T> & v)
{
    std::vector<int> vector1;
    unsigned i, last = v.size();

    for(int j = 0; j <= v.size(); j++)
    {
        std::cout << seqVectSearch(v, j, last, j);
        if(seqVectSearch(v, j, last, j) != v[i])
            vector1.push_back(seqVectSearch(v, j, last, j));
    }
}

template <typename T>
unsigned seqVectSearch(const std::vector<T> & v, unsigned first,
                       unsigned last, const T& target)
{
    unsigned i = first;
    while((v[i] != target) && (v[i] <= last))
    {
        if(v[i] == target)
            return i;
        i++;
    }
    return last;
}

template <typename T>
void writeVector(const std::vector<T> & v)
{
    unsigned i;
    unsigned n = v.size();

    for (i = 0; i < n; i++)
        std::cout << v[i] << ' ';
    std::cout << std::endl;
}

the final file for this program is "vectorDriver.cpp" but this one has no errors. This one just runs the program by calling the functions:

#include "vectorAux.h"
#include <vector>
#include <iostream>

void fillVector(std::vector<int> & vect);

int main()
{
  using namespace std;

  vector<int> vect;

  fillVector(vect);
  cout << "Testing removeDup" << endl;
  cout << "Original vector is  ";
  writeVector(vect);

  removeDup(vect);
  cout << "Vector with duplicates removed is  ";
  writeVector(vect);
  cout << endl;
  writeVector(vect);

  return 0;
}

void fillVector(std::vector<int> & vect)
{
  int arr[] = {1,7,2,7,9,1,2,8,9};
  unsigned arrsize = sizeof(arr)/sizeof(int);

  vect = std::vector<int>(arr, arr+arrsize);
}

I would really appreciate any and all help/advice that is given! I have looked around for a while and each source that I have found has said to guard the header file, but I have already done that and my problem still ensues.

Answer

john picture john · Oct 23, 2012

You include vectorAux.cpp in vectorAux.h. I would guess that you are also compiling vectorAux.cpp separately. So you end up compiling the code in vectorAux.cpp twice.

Answer is simple, move the code from vectorAux.cpp to vectorAux.h, delete vectorAux.cpp, you don't need it.

Template code almost always goes in header files.