我读了许多关于这个链接器错误的其他问题,但我还是无法解决它...
vector.hpp
#ifndef vector_hpp
#define vector_hpp
namespace vec
{
template<class T> class Vector
{
private:
T* arr;
unsigned int numberOfElements;
public:
Vector();
~Vector();
void push_back(const T&);
void pop_back();
void pop_front();
unsigned int size() const;
T& at(int);
T& front();
bool empty() const;
T& operator[](const int) const;
};
}
#endif
vector.cpp(不是一切,都会太长)
#include "vector.hpp"
#include <iostream>
namespace vec
{
template<class T>
Vector<T>::Vector()
: arr(nullptr), numberOfElements(0)
{
return;
}
template<class T>
Vector<T>::~Vector()
{
delete[] arr;
}
template<class T>
unsigned int Vector<T>::size() const
{
return numberOfElements;
}
...
}
patientenliste.hpp
#ifndef patientenliste_hpp
#define patientenliste_hpp
#include "vector.hpp"
#include <iostream>
#include "patient.hpp"
using namespace std;
class Patientenliste
{
private:
vec::Vector<Patient> liste;
public:
Patientenliste& operator+= (const Patient&);
vec::Vector<Patient>& getPListe();
friend ostream& operator<< (ostream&, const Patientenliste&);
};
ostream& operator<< (ostream&, const Patientenliste&);
#endif
patientenliste.cpp
#include "patientenliste.hpp"
Patientenliste& Patientenliste::operator+= (const Patient& p)
{
liste.push_back(p);
return *this;
}
vec::Vector<Patient>& Patientenliste::getPListe()
{
return liste;
}
ostream& operator<< (ostream& os, const Patientenliste& p)
{
if(p.liste.size() == 0)
os << "Keine Patienten in der Liste.";
else
for(unsigned int i=0; i < p.liste.size(); i++)
os << p.liste[i] << endl;
return os;
}
我的项目中有更多的文件/类,但每个使用我的Vector类的类都会出现链接错误:
patientenliste.cpp :(。text + 0x32):Nicht definierter Verweis auf vec::Vector<Patient>::size() const'
patientenliste.cpp:(.text+0x5e): Nicht definierter Verweis auf
vec :: Vector :: size()const&#39;
patientenliste.cpp :(。text + 0x6c):Nicht definierter Verweis auf`vec :: Vector :: operator [](int)const&#39;
(英文&#34; Nicht definierter Verweis auf&#34;表示&#34;未定义参考&#34;) 我完全不知道出了什么问题...... 我已经尝试过命名空间vec,但那也没有用。
答案 0 :(得分:0)
模板定义应位于定义相应模板类的同一头文件中。