有史以来第一个问题,这里是:
我已将模板类拆分为标头和源文件。在头文件的底部我包括源文件,我现在在源文件上使用#ifndef / #define / #endif guards。许多人向其他人建议我应该使用内联来定义我的功能;这是一个家庭作业,不允许。我也读过表明我不应该使用.cpp源文件的答案,而是使用像.tcc / .tcp这样的东西,以便IDE知道它们是模板源文件,但这并不是&#39工作要么。
我实际上通过在主文件中包含源文件而不是标题来实现这一点,但我知道这不是我应该做的事情。
本网站上我试图解决问题的文章包括:
Including .cpp file in the header file for a template class What is the correct file extension for a C++ template implementation file? Splitting templated C++ classes into .hpp/.cpp files--is it possible? C++ Unresolved external symbol with Class templates Why do I get "unresolved external symbol" errors when using templates?
这似乎是一个简单的问题并且已经解决了一百次,所以为什么我的代码不会做它的事情呢?感谢您的帮助,我的代码发布在下面。
标题文件:
//This file contains the declarations and signatures for Array class
//members and functions. The class is used to manage memory
#ifndef ARRAY_HPP
#define ARRAY_HPP
#include "Point.hpp" //the array is of points and needs the Point.hpp file
namespace DanWhite
{
namespace Containers
{
template <class T> class Array
{
private:
T* pointarray; //the dynamic array itself
int size; //the size of the array
public:
Array(); //creates a dynamic array of 10 points
Array(int arraysize); //creates a dynamic array of <arraysize> points
Array(const Array<T> &AnotherArray); //creates a dynamic array exactly the same as another
~Array(); //destructor
int Size() const; //returns the size of the array
void SetElement(int element,
const T& point); //sets a specific element in the array
T GetElement(int element) const; //returns a specific element in the array
Array<T>& operator=(const Array<T>& source); //sets the points of an array equal to another
const T& operator[](int element) const; //allows reading an element in an array
T& operator[](int element); //allows writing to an element in an array
};
}
}
#ifndef ARRAY_CPP
#define ARRAY_CPP
#include "Array.cpp"
#endif
#endif
源文件
//this source file implements all of the functions and operators in the
//Array class header file
#ifndef ARRAY_CPP
#define ARRAY_CPP
#include "Point.hpp"
#include "OOBE.hpp"
#include "Array.hpp"
#include <sstream>
using namespace DanWhite::Containers;
template <class T>
Array<T>::Array() : size(10) //default constructor
{
std::cout << "Array default constructed"
<< std::endl;
pointarray = new T[10];
}
template <class T>
Array<T>::Array(int arraysize) //specific constructor with size
{
std::cout << "Array specific constructed"
<< std::endl;
size = arraysize;
pointarray = new T[size];
}
template <class T>
Array<T>::Array(const Array<T>& anotherarray) //copy constructor
{
std::cout << "Array<T> copy constructed"
<< std::endl;
size = anotherarray.size;
pointarray = new T[size];
for (int i = 0; i < anotherarray.size; i++)
{
pointarray[i] = anotherarray.pointarray[i];
}
}
template <class T>
Array<T>::~Array() //destructor
{
std::cout << "Array destructed" << std::endl;
delete [] pointarray;
}
template <class T>
int Array<T>::Size() const //returns the size of the array object
{
return size;
}
template <class T>
void Array<T>::SetElement(int element, //changes an element of an array object
const T& t)
{
if (element > (size-1) || element < 0)
{
throw OOBE(element);
}
pointarray[element] = t;
}
template <class T>
T
Array<T>::GetElement(int element) const //returns an element of an array object
{
if (element > (size-1) || element < 0)
{
throw OOBE(element);
}
return pointarray[element];
}
template <class T>
Array<T> & Array<T>::operator=(const Array<T> & source) //assigns the values of one array object
//to another array object
{
if (this == &source)
{
return *this;
}
for (int i = 0; i < source.Size(); i++)
{
pointarray[i] = source.pointarray[i];
}
return *this;
}
template <class T>
const T
& Array<T>::operator[](int element) const //returns an element of an array
{
if (element > (size - 1) || element < 0)
{
throw OOBE(element);
}
else
{
return pointarray[element];
}
}
template <class T>
T & Array<T>::operator[](int element) //allows changing an element of an array
{
if (element > (size - 1) || element < 0)
{
throw OOBE(element);
}
else
{
return pointarray[element];
}
}
#endif
再次感谢您的帮助。经过几天试图解决这个问题和阅读文章,我很高兴把它放在我身后。