在c ++中创建一个新的模板容器类型
好的,所以我试图在c ++中实现模板化的循环双向链表。我遇到的问题是,当我尝试将类定义和函数分别分别放入.h和.cpp文件时,编译器不断向我吐出关于没有模板参数的错误。
这是头文件cdl_list.h
#ifndef CDL_LIST_H
#define CDL_LIST_H
#include <iostream>
using namespace std;
template <typename T>
class cdl_list {
public:
cdl_list(){
first = last = current = NULL;}; // constructor, "current" = "null"
~cdl_list(); // deconstructor
void insertFromFront (T &); // inserts an element of type T in the front properly
void insertFromBack (T &); // inserts an element of type T in the back properly
void deleteFront(); // removes the first element in the list, updating relevant pointers
void deleteBack(); // removes the last element in the list, updating relevant pointers
void reset(); // makes the "current" pointer the front element
void next(); // makes the "current" pointer the next node neighbor
T currentValue(); // return the data in the node that "current" refers to
void deleteCurrent(); // delete the node that the current pointer refers to; current = old -> next
bool isEmpty(); // returns true if and only if the list is empty
void print(); // displays the current data in the linked list
private:
struct listNode* first;
struct listNode* last;
struct listNode* current;
protected:
struct listNode {
listNode* prev; // "previous" pointer
T data; // data in the node
listNode* next; // "next" pointer
};
};
#include "cdl_list.h"
#include <iostream>
using namespace std;
//And here is the .cpp file, what I have so far, because I can't even get just this one function to compile
template < typename T > void cdl_list::insertFromFront(const T &frontInsert) {
listNode *oldFirst;
oldFirst = (listNode *) malloc(sizeof(listNode));
oldFirst = first;
oldFirst -> prev = frontInsert;
while(current -> next != first)
current = current -> next;
frontInsert -> prev = current;
current -> next = frontInsert;
first = frontInsert;
}
#endif
没有找到相关结果
已邀请:
4 个回复
席酱
抹持奠糙驰
声明时,实际上都需要编译器生成一个全新的类。如果不知道如何实现整个新类,它就无法编译新类。这就是你的编译器吐出你看到的错误的原因。 更具描述性。我们假设我创建了一个名为“bleah.h”的文件
现在我在“Yuck.h”中有这个:
由于我在这里有标题,我需要标题中的所有信息来编译“Foo”的不同模板化版本。
涸坍饺
正如其他海报所指出的那样,你应该把放在.cpp中的东西放在.inl文件中,并在.h文件的末尾做#include“myfile.inl”,并避免使用.cpp文件作为模板。
遣莫捅炭