任何人都可以告诉我这段代码有什么问题。我收到了错误
1>Pointers.obj : error LNK2019: unresolved external symbol "public: __thiscall TArray<int>::~TArray<int>(void)" (??1?$TArray@H@@QAE@XZ) referenced in function _wmain
1>Pointers.obj : error LNK2019: unresolved external symbol "public: void __thiscall TArray<int>::Add(int)" (?Add@?$TArray@H@@QAEXH@Z) referenced in function _wmain
1>Pointers.obj : error LNK2019: unresolved external symbol "public: __thiscall TArray<int>::TArray<int>(void)" (??0?$TArray@H@@QAE@XZ) referenced in function _wmain
1>C:\Visual Studio 2005\Projects\Pointers\Pointers\Debug\Pointers.exe : fatal error LNK1120: 3 unresolved externals
我尝试使用模板创建列表。这可能是一个可能重复的问题,如果很遗憾,但是未解决的符号错误原因往往有不同的来源。根据visual studio,TArray析构函数没有定义,但你可以在scData.cpp上看到它。我的示例基于http://www.cplusplus.com/articles/E3wTURfi/示例。
scData.h
#include "stdafx.h"
#ifndef SC_DATA_H
#define SC_DATA_H
template <typename T>
class TArray;
template <typename T>
class TDataNode //nodes to be contained with a list
{
friend class TArray<T>;
private:
T data;
TDataNode* nextNode;
TDataNode* prevNode;
public:
TDataNode(T);
T getData();
};
template <typename T>
class TArray
{
private:
TDataNode<T> *startPtr; //stores the pointer of first object in the linked list
TDataNode<T> *endPtr; //stored the pointer of the last object in the linked list
long FSize;
bool isEmpty(); //utility functions used to see if the list contains no elements
public:
TArray();
~TArray();
void Add(T);
void Remove(long);
TDataNode<T>* Search(T);
TDataNode<T>* Item(long);
long Size(void);
void Reset();
void Clear();
};
#endif
scData.cpp
#ifndef NULL
#define NULL 0L
#endif
#include "scData.h"
template <typename T>
TDataNode<T>::TDataNode(T dataIn)
{
data = dataIn; //stores data in node
nextNode = NULL;
prevNode = NULL
}
template <typename T>
T TDataNode<T>::getData() //returns data stored in node
{
return data;
}
template <typename T>
TArray<T>::TArray()
{
startPtr = NULL;
endPtr = NULL;
FSize = 0;
}
template <typename T>
TArray<T>::~TArray()
{
if ( !isEmpty() ) // List is not empty
{
TDataNode<T> *currentPtr = startPtr;
TDataNode<T> *tempPtr;
while ( currentPtr != 0 ) // delete remaining nodes
{
tempPtr = currentPtr;
currentPtr = currentPtr->nextNode;
delete tempPtr;
}
}
}
template <typename T>
bool TArray<T>::isEmpty()
{
if(startPtr == NULL && endPtr == NULL) //if the start pointer and end pointer are NULL then the list is empty
return 1;
else
return 0;
}
template <typename T>
void TArray<T>::Clear()
{
Reset();
}
template <typename T>
void TArray<T>::Reset()
{
if ( !isEmpty() ) // List is not empty
{
TDataNode<T> *currentPtr = startPtr;
TDataNode<T> *tempPtr;
while ( currentPtr != 0 ) // delete remaining nodes
{
tempPtr = currentPtr;
currentPtr = currentPtr->nextNode;
delete tempPtr;
}
startPtr=NULL;
endPtr=NULL;
FSize=0;
}
}
template <typename T>
void TArray<T>::Add(T dataIn)
{
if(isEmpty()) //if the list is empty create first element of the list
{
TDataNode<T> * newPtr = new TDataNode<T>(dataIn); //creates new node
startPtr = newPtr; //start and end pointer are same becuase there is only one object in list
endPtr = newPtr;
}else //if node(s) exist in list insert additional object after the last
{
TDataNode<T> * newPtr = new TDataNode<T>(dataIn);
newPtr->prevNode=endPtr;
endPtr->nextNode=newPtr;//the next pointer of the previous last node points to the new node
endPtr = newPtr; //new node is now the ending node
//oringinal code insert additional object before the first
//newPtr->nextNode = startPtr; //the next pointer of the new node points to the node that was previously first
//startPtr = newPtr; //the pointer for the new node is now the starting node
}
FSize++;
}
template <typename T>
void TArray<T>::Remove(long index)
{
TDataNode<T> *currentPtr = startPtr;
long tempIndex=0L;
if ( !isEmpty() ) // List is not empty
{
currentPtr = startPtr;
while ( currentPtr != 0 ) // delete remaining nodes
{
if (index=tempIndex)
{
if (index=0) //delete first node
{
startPtr=currentPtr->nextNode;
currentPtr->nextNode->prevNode=NULL;
delete currentPtr;
}
else if(currentPtr->nextNode=NULL) //delete last Node
{
currentPtr->prevNode->nextNode=NULL;
endPtr=currentPtr->prevNode;
delete currentPtr;
}
else
{
currentPtr->prevNode->nextNode=currentPtr->nextNode;
currentPtr->nextNode->prevNode=currentPtr->prevNode;
delete currentPtr;
}
FSize--;
}
tempIndex++;
currentPtr = currentPtr->nextNode;
}
}
return;
}
template <typename T>
TDataNode<T>* TArray<T>::Item(long index)
{
TDataNode<T>* DataNode;
long tempIndex=0L;
if ( (!isEmpty())||(index>FSize-1)) // List is not empty
{
DataNode = startPtr;
while ( DataNode != 0 ) // delete remaining nodes
{
if (index=tempIndex)
{
return DataNode;
}
DataNode=DataNode->nextNode;
tempIndex++;
}
return NULL;
}
return NULL;
}
template <typename T>
long TArray<T>::Size(void)
{
return FSize;
}
template <typename T>
TDataNode<T>* TArray<T>::Search(T key) //search functions that searches for node that contains data equal to the key
{
TDataNode<T>* nodePtr;
bool found = false;
nodePtr = startPtr;
while((!found) && (nodePtr != NULL)) //runs through list until data is found within a node or end of list is reached
{
if(nodePtr->getData() == key) //if the node's data equals the key then the node has been found
found = true;
else
nodePtr = nodePtr->nextNode; //moves to next node in list
}
return nodePtr; //returns pointer to the node that contains data equal to key (NULL if not found)
}
的main.cpp
#include <iostream>
#include <exception>
#include "stdafx.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
TArray<int> intList; //creates order linked list
for(int i = 0; i <= 10; i++)
{
intList.Add(1+10*i); //inserts value from array to the linked list in the proper positions
}
return 0;
}