Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redefinition of template<class T> in C++

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.

like image 678
Vincent Giuliana Avatar asked Oct 23 '12 07:10

Vincent Giuliana


3 Answers

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.

like image 115
john Avatar answered Nov 15 '22 23:11

john


The contents of your "VectorAux.cpp" should be inside the "VectorAux.h" because you define a template class.

like image 31
alestanis Avatar answered Nov 16 '22 00:11

alestanis


The simple answer is: Templates should not be split into source and header files. Keep it all in the header file when using templates.

like image 2
RedX Avatar answered Nov 15 '22 23:11

RedX