redefinition of template<class T> in C++

前提是你 提交于 2021-01-27 04:18:31

问题


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.


回答1:


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.




回答2:


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




回答3:


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




回答4:


Remove your .cpp of your templated class from your project source files. You are currently compiling the .cpp file twice; once because it is in your project and secondly because your .h is including it. Also, remove the .h inclusion from your .cpp, you don't need it since the header is including the .cpp at the bottom. This is one of the unfortunate problems of separating out templated classes.




回答5:


The error happens during the compilation of the vectorAux.cpp file, because you are including the header file, which in turn includes the implementation file. This way, you end up with the content of the cpp file being duplicated.

If you really do want to split the implementation and declaration of the template functions into two separate files, there are two things you should do:

  1. Don't include the headerfile in the implementation file.
  2. Don't add the cpp file to the files being translated by the compiler.

Any one of those two options will get rid of your compiler error, but you really should do both.



来源:https://stackoverflow.com/questions/13025266/redefinition-of-templateclass-t-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!