我有一个模板化的类(Vector),我正试图开始工作。我不断得到编译器,比如当我尝试创建一个对象时
1>Project 2.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<long>::Vector<long>(void)" (??0?$Vector@J@@QAE@XZ) referenced in function _main
1>Project 2.obj : error LNK2019: unresolved external symbol "public: __thiscall Vector<long>::~Vector<long>(void)" (??1?$Vector@J@@QAE@XZ) referenced in function _main
fatal error LNK1120: 2 unresolved externals
我之前从未使用模板化课程,并且对于如何使其正常工作非常困惑。
Vector.h
#ifndef VECTOR_H
#define VECTOR_H
#include "stdafx.h"
template <typename T>
class Vector
{
private:
// Constants
// The default size for the default constructor
static const int defaultSize = 20;
// Variables
// Pointer to first element in array for this vector
T* vectorArray;
// Current size (size of vector array) of the array for this vector
int vectorSize;
// Current number of elements stored in vector
int vectorElementCount;
public:
// Constructors/Deconstructor
// Default (empty) constructor
Vector(void);
// Constructor with predetermined size
Vector(int size);
// Copy constructor
Vector(const Vector<T>& old);
// Deconstructor
~Vector();
// Private Helper Methods
// Doubles the amount of storage in the vector
void expand();
// Shifts data from a certain index to the right a set number of shifts
void shiftRight(int index, int shifts);
// Shifts data from a certain index to the left a set number of shifts
void shiftLeft(int index, int shifts);
// Public Methods
// Returns the current size (size of vector array) of the vector
int getVectorSize();
// Returns the number of elements in the vector
int getVectorElementCount();
// Gets element at specified index
T& getAtIndex(int index);
// Sequentually prints all the elements stored in the vector
void printVector();
// Vector Manipulation Methods
// Adds a new value to the end of the vector
void add(T newVal);
// Ammends the value at the specified index with the value
void set(int index, T newVal);
// Inserts the new value at the specified index and shifts the vector 1 shift to the right
void insert(int index, T newVal);
// Shifts all elements beyond one to the left, deleting the element at the index
void deleteItem(int index);
// Vector helper methods
// Checks if the index exists within the vector
bool isInBounds(int index);
// Deletes pointers from memory. MUST BE A VECTOR OF NON-ARRAY POINTERS
void deletePointers();
// Deletes array pointers from memory. MUST BE A VECTOR OF ARRAY POINTERS
void deleteArrayPointers();
// Operator Overloaders
T& operator[](int index);
};
#endif
Vector.cpp
#include "stdafx.h"
#include "Vector.h"
#include <iostream>
// Constructors/Deconstructor
// Default (empty) constructor
template <typename T>
Vector<T>::Vector(void)
{
vectorElementCount = 0;
vectorSize = defaultSize;
vectorArray = new T[defaultSize];
}
// Constructor (non empty)
template <typename T>
Vector<T>::Vector(int size)
{
vectorElementCount = 0;
vectorSize = size;
vectorArray = new T[size];
}
// Copy constructor
template <typename T>
Vector<T>::Vector(const Vector<T>& old)
{
arraySize = old.arraySize;
vectorElementCount = old.vectorElementCount;
vectorArray = new T[arraySize];
// Copies data from old to new
for(int i=0; i < getVectorElementCount; i++)
{
vectorArray[i] = old.vectorArray[i];
}
}
// Deconstructor
template <typename T>
Vector<T>::~Vector()
{
delete vectorArray;
}
// Private Helper Methods
// Doubles the amount of storage in the vector
template <typename T>
void Vector<T>::expand()
{
// Create new array with 2x the size
T* newVectorArray = new T[arraySize*2];
// Copy the data over to the new array
for(int i=0; i<vectorElementCount; i++)
{
newVectorArray[i] = vectorArray[i];
}
vectorSize *= 2;
delete vectorArray;
vectorArray = newVectorArray;
}
// Shifts data from a certain index to the right a set number of shifts
template <typename T>
void Vector<T>::shiftRight(int index, int shifts)
{
// Expand the vector if there isn't enough room
while(arraySize < vectorElementCount + shifts)
{
expand();
}
// Shifts and prepares for new data to occupy the spots
for(int i=vectorElementCount - 1; i >= index; i--)
{
vectorArray[i + places] = vectorArray[i];
vectorArray[i] = NULL;
}
}
// Shifts data from a certain index to the left a set number of shifts
template <typename T>
void Vector<T>::shiftLeft(int index, int shifts)
{
// There cannot be more shifts than there are elements
if(vectorElementCount < shifts)
throw 0; // TODO THROW EXCEPTION
if(vectorElementCount == 1)
vectorArray[0] = NULL; // There's only one item in the vector
else
{
// Shift the elements down
for(int i=index; i < (vectorElementCount - shifts); i++)
{
vectorArray[i] = vectorArray[i + shifts];
vectorArray[i + shifts] = NULL;
}
}
}
// Public Methods
// Returns the current size (size of vector array) of the vector
template <typename T>
int Vector<T>::getVectorSize()
{
return this.vectorSize;
}
// Returns the number of elements in the vector
template <typename T>
int Vector<T>::getVectorElementCount()
{
return this.vectorElementCount;
}
// Gets element at specified index
template <typename T>
T& Vector<T>::getAtIndex(int index)
{
if(index > vectorElementCount)
throw 0; // TODO THROW EXCEPTION
return vectorArray[index];
}
// Sequentually prints all the elements stored in the vector
template <typename T>
void Vector<T>::printVector()
{
for(int i=0; i<getVectorElementCount; i++)
cout << i << ". " << vectorArray[i] << endl;
}
// Vector Manipulation Methods
// Adds a new value to the end of the vector
template <typename T>
void Vector<T>::add(T newVal)
{
// Expand if the size of the vector is exceeded
if(vectorElementCount >= vectorSize)
expand();
vectorArray[vectorElementCount] = newVal;
++vectorElementCount;
}
// Ammends the value at the specified index with the value
template <typename T>
void Vector<T>::set(int index, T newVal)
{
if(!isInBounds(index))
throw 0; // TODO THROW EXCEPTION
vectorArray[index] = newVal;
}
// Inserts the new value at the specified index and shifts the vector 1 shift to the right
template <typename T>
void Vector<T>::insert(int index, T newVal)
{
if(!isInBounds(index))
throw 0; // TODO THROW EXCEPTION
// Make room for the new value
shiftRight(index, 1);
// An element was added
++vectorElementCount;
set(index, value);
}
// Shifts all elements beyond one to the left, deleting the element at the index
template <typename T>
void Vector<T>::deleteItem(int index)
{
// Shift over the element at the index once
shiftLeft(int index, 1);
// The element was removed
--vectorElementCount;
}
// Vector helper methods
// Checks if the index exists within the vector
template <typename T>
bool Vector<T>::isInBounds(int index)
{
return (index >= 0 && index <= (vectorElementCount - 1))
}
// Deletes pointers from memory. MUST BE A VECTOR OF NON-ARRAY POINTERS
template <typename T>
void Vector<T>::deletePointers()
{
for (int i=0; i < vectorElementCount; i++)
{
delete vectorArray[i];
}
}
// Deletes array pointers from memory. MUST BE A VECTOR OF ARRAY POINTERS
template <typename T>
void Vector<T>::deleteArrayPointers()
{
for (int i=0; i < vectorElementCount; i++)
{
delete[] vectorArray[i];
}
}
// Operator Overloaders
template <typename T>
T& Vector<T>::operator[](int index)
{
return this.getAtIndex(index);
}
如果有人能告诉我我做错了什么,我会永远感激。
注意:Main.cpp有#include“Vector.h”