我正在编写一个实现双向链表的程序。我的问题是,当我编译时,通过发出命令
g++ -g -Wall DynamicSequenceVector.cpp DynamicSequenceVector.h main.cpp
我收到以下控制台输出
/tmp/cc6P5VZK.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `DynamicNode::DynamicSequenceVector<int>::DynamicSequenceVector(int)'
main.cpp:(.text+0x3a): undefined reference to `DynamicNode::DynamicSequenceVector<int>::~DynamicSequenceVector()'
main.cpp:(.text+0x4f): undefined reference to `DynamicNode::DynamicSequenceVector<int>::~DynamicSequenceVector()'
collect2: error: ld returned 1 exit status
我感觉这是我在main.cpp中导入文件的问题,因为如果我将main函数移动到DyanmicSequenceVector.cpp文件中,它编译完全正常。另外,在构造带参数的新对象时,我只收到这些编译错误。
DynamicSequenceVector.h
#ifndef __DYNAMIC_VECTOR
#define __DYNAMIC_VECTOR
namespace DynamicNode {
template <class Type>
class DynamicSequenceVector {
private:
struct dynamicNode {
dynamicNode *previousLink;
dynamicNode *nextLink;
Type data;
int position;
};
int nodeCount;
int currentPosition;
dynamicNode *headNode;
dynamicNode *tailNode;
dynamicNode *currentNode;
dynamicNode *tempNode;
public:
DynamicSequenceVector();
DynamicSequenceVector(Type data);
~DynamicSequenceVector();
void appendNode(Type nodeData);
void accessData(int startingPosition, int endingPosition);
};
}
#endif
DynamicSequenceVector.cpp
#include <iostream>
#include "DynamicSequenceVector.h"
using namespace std;
using namespace DynamicNode;
template <typename Type>
DynamicSequenceVector<Type>::DynamicSequenceVector() {
nodeCount = 0;
currentPosition = NULL;
headNode = NULL;
tailNode = NULL;
currentNode = NULL;
}
template <typename Type>
DynamicSequenceVector<Type>::DynamicSequenceVector(Type nodeData) {
nodeCount = 1;
currentPosition = 0;
headNode = new dynamicNode;
headNode->previousLink = NULL;
headNode->nextLink = NULL;
headNode->data = nodeData;
headNode->position = 0;
currentNode =
}
template <typename Type>
DynamicSequenceVector<Type>::~DynamicSequenceVector() {
while(nodeCount != 0) {
tempNode = tailNode->previousLink;
delete tailNode;
tailNode = tempNode;
}
return;
}
template <typename Type>
void DynamicSequenceVector<Type>::appendNode(Type nodeData) {
if (currentPosition == 0) {
headNode = new dynamicNode;
headNode->data = nodeData;
headNode->position = 0;
headNode->previousLink = NULL;
headNode->nextLink = NULL;
} else {
tempNode = new dynamicNode;
tempNode->data = nodeData;
tempNode->previousLink = tailNode;
tempNode->position = nodeCount + 1;
tailNode->nextLink = tempNode;
tailNode = tempNode;
}
nodeCount++;
}
template <typename Type>
void DynamicSequenceVector<Type>::accessData(int startingPosition,
int endingPosition) {
cout << "Data accessed";
return 0;
}
的main.cpp
#include <iostream>
#include "DynamicSequenceVector.h"
//using namespace std;
using namespace DynamicNode;
int main() {
DynamicSequenceVector<int> test();
DynamicSequenceVector<int> testingVector(5); // gives an error
//test = new DynamicSequenceVector<char>::DynamicSequenceVector();
std::cout << "Hello world!\n";
}
答案 0 :(得分:3)
模板成员的实现必须位于标题中,而不是单独的.cpp文件中。
编译main.cpp时,编译器需要实现可见才能实例化DynamicSequenceVector<int>
,而这些实现不可用。因此,编译器假定模板实例化在另一个编译单元中可用,但事实并非如此,这就是链接器失败的原因。
(DynamicSequenceVector.cpp文件在这里甚至没有做任何有用的事情 - 未实例化的模板成员实际上从未在目标文件中写出,因为这没有任何意义。将内容移动到头文件中然后删除.cpp文件是解决此问题的正确方法。)
或者,您可以将其添加到DynamicSequenceVector.cpp的底部:
template class DynamicSequenceVector<int>;
这将指示编译器实例化此版本的模板类,并使其在该编译单元中可用并从该编译单元导出。然后当链接器去解析main的编译单元中的符号时,它将能够找到它们。
但是,这意味着您需要维护此模板类的每个实例的集中列表。这是很多工作,通常被认为是一个坏主意。
答案 1 :(得分:1)
你不能在cpp中使用模板化代码实现,因为main只知道h文件..你应该将模板化实现移动到h文件