我正在尝试编写一些容器类以在C ++中实现主要数据结构。头文件在这里:

#ifndef LINKEDLIST1_H_
#define LINKEDLIST1_H_

#include <iostream>
using namespace std;

template<class T> class LinkedList1;
template<class T> class Node;

template<class T>
class Node
{
    friend class LinkedList1<T> ;
public:
    Node<T> (const T& value)
    {
        this->Data = value;
        this->Next = NULL;
    }
    Node<T> ()
    {
        this->Data = NULL;
        this->Next = NULL;
    }
    T Data;
    Node* Next;
};

template<class T>
class LinkedList1
{
    friend class Node<T> ;
public:
    LinkedList1();
//    LinkedList1<T>();
    ~LinkedList1();
    // Operations on LinkedList
    Node<T>* First();

    int Size();
    int Count();
    bool IsEmpty();
    void Prepend(Node<T>* value); //O(1)
    void Append(Node<T>* value);
    void Append(const T& value);
    void Insert(Node<T>* location, Node<T>* value); //O(n)
    Node<T>* Pop();
    Node<T>* PopF();
    Node<T>* Remove(const Node<T>* location);
    void Inverse();
    void OInsert(Node<T>* value);
    // TODO Ordered insertion. implement this: foreach i,j in this; if i=vale: i+=vale, break; else if i<=value<=j: this.insert(j,value),break
    void print();
private:
    Node<T>* first;
    int size;
};

#endif /* LINKEDLIST1_H_ */


当我尝试在另一个类中使用它时,例如:

void IDS::craete_list()
{
    LinkedList1<int> lst1 = LinkedList1<int>::LinkedList1<int>();
}


发生此错误:

undefined reference to 'LinkedList1<int>::LinkedList1<int>()'


该类的构造函数是公共的,并且包含其头文件。我也尝试包括该类的.cpp文件,但这没有帮助。我以完全相同的方式编写了其他类,例如SparseMatrix和DynamicArray,并且没有错误!

最佳答案

请参见FAQ item 35.12 "Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?"

这很可能就是您遇到的问题。

干杯,……

关于c++ - 这是什么错误? (为什么它不出现在其他类中?),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4041000/

10-14 15:18
查看更多