当我尝试使用结构模板作为agregation时,为什么会出现以下错误?

时间:2017-04-02 13:10:22

标签: c++ visual-studio templates struct linked-list

我要做的是使用链接列表在c ++中模拟java中的ArrayList,作为学习即将到来的考试的一种方式。

这是我到目前为止所写的内容。

这是头文件......

#ifndef ArrayList_H
#define ArrayList_H
#include "ArrayListNode.h"
template<class T>
class ArrayList
{
private:
    ArrayListNode<T>* head;
    int length;

public:
    ~ArrayList();        //Destructor.
    ArrayList();         //Constructor.
    void add(T);        //Append an element at the last position.
    void display(); //Display all element in the list.
    void remove(int);   //Remove element at specify index
    void clear();       //Remove all the elements from the list.
    T get(int);         //Return the element at the specified position.
    bool isEmpty();     //Return true if this list contains no elements.
    int size();         //Return the length of the list.

};
#endif

现在这是实施文件:

template<class T>
ArrayList<T>::ArrayList()
{
    head = nullptr;
    length = 0;
}

template<class T>
ArrayList<T>::~ArrayList()
{
    ArrayListNode<T>* traverse = head;
    ArrayListNode<T>* nextNode;

    while (traverse != nullptr)
    {
        nextNode = traverse->next;
        delete traverse;
        traverse = nextNode;
    }
}

template<class T>
void ArrayList<T>::add(T item)
{
    if (head == nullptr)
    {
        head = new ArrayListNode;
        head->item = item;
        head->next = nullptr;
    }
    else
    {
        ArrayListNode<T>* traverse = head;  
        ArrayListNode<T>* newNode = new ArrayListNode;
        newNode->item = item;
        newNode->next = nullptr;

        //Find the last node in the list
        while (traverse->next)
        {
            traverse = traverse->next;
        }
        traverse->next = newNode;
    }
}

template<class T>
void ArrayList<T>::display()
{
    if (head == nullptr)
        return;
    else
    {
        ArrayListNode<T>* traverse = head;
        while (traverse->next)
        {
            cout<< traverse->item << endl;
            traverse = traverse->next;
        }
    }
}
template<class T>
void ArrayList<T> ::remove(int index)
{

}
template<class T>
void ArrayList<T>::clear()
{

}
template<class T>
T ArrayList<T>::get(int index)
{

    return;
}

template<class T>
bool ArrayList<T>::isEmpty()
{

}
template<class T>
int ArrayList<T>::size()
{
    return length;
}

到目前为止......我只创建了显示列表中项目的构造函数,析构函数,显示。我还创建了附加项目的函数add 在最后一个位置。其他方法都是空的。但是我从添加功能中收到错误。

这是类的结构......

template<class T>
struct ArrayListNode
{
    T item;                 //To hold any data.
    ArrayListNode<T>* next; //To point to the next node
};

这是班级的主要内容......

#include <iostream>
#include "ArrayList.h"
#include "ArrayList.cpp"
using namespace std;
int main()
{
    ArrayList<int> list;

    list.add(5);
    list.display();

    return 0;
}

现在当我在没有add函数的情况下编译程序时,它正确编译但是,一旦我调用add函数,我得到以下错误。

c:\users\julio\documents\visual studio 2017\projects\arraylist\arraylist.cpp(31): error C2955: 'ArrayListNode': use of class template requires template argument list
1>c:\users\julio\documents\visual studio 2017\projects\arraylist\arraylistnode.h(5): note: see declaration of 'ArrayListNode'
1>c:\users\julio\documents\visual studio 2017\projects\arraylist\arraylist.cpp(28): note: while compiling class template member function 'void ArrayList<int>::add(T)'
1>        with
1>        [
1>            T=int
1>        ]
1>c:\users\julio\documents\visual studio 2017\projects\arraylist\main.cpp(9): note: see reference to function template instantiation 'void ArrayList<int>::add(T)' being compiled
1>        with
1>        [
1>            T=int
1>        ]
1>c:\users\julio\documents\visual studio 2017\projects\arraylist\main.cpp(7): note: see reference to class template instantiation 'ArrayList<int>' being compiled
1>c:\users\julio\documents\visual studio 2017\projects\arraylist\arraylist.cpp(31): error C2512: 'ArrayListNode': no appropriate default constructor available
1>c:\users\julio\documents\visual studio 2017\projects\arraylist\arraylist.cpp(38): error C2512: 'ArrayListNode': no appropriate default constructor available

0 个答案:

没有答案