1

这是没有标题保护和其他功能的 Graph.h 的内容

template <class T> class Node{
public:
    T data;
    Node<T> *NextNode;
public:
    Node();
    Node(T a);
    T getValue();
    void setValue(T a);
    void chainNode(Node<T> a);
    void chainNode(Node<T> *a);
    Node<T> getNextNode();
    void unchainNode();
};
//related methods
template <class T> Node<T>::Node(){
    data = NULL;
    NextNode = NULL;
}    
template <class T> void Node<T>::chainNode(Node<T> a){
    NextNode = NULL;
    NextNode = &a;
}
template <class T> void Node<T>::chainNode(Node<T> *a){
    NextNode = NULL;
    NextNode = a;
}    

template <class T> class List{
public:
    Node<T> *Head;
    List(Node<T> a);
    void AddInFront(Node<T> a);
    void AddInFront(Node<T> *a);
    void Append(Node<T> a);
    bool Remove(Node<T> a);
    bool Remove(T a);
    bool Contains(T a);
    bool DeleteList();
};
//Only working method of List
template <class T> List<T>::List(Node<T> a){
Head = &a;
}
// Error occurs in this Function
template <class T> List<T>::AddInFront(Node<T> a){
    a.chainNode(Head);
    Head = NULL;
    Head = &a;
}        

这是我的主要

#include<iostream>
#include"Graph.h"
int main(){
    Node<int> a = Node<int>(20);
    List<int> d = List<int>(a);
    Node<int> b = Node<int>(20);
    d.AddInFront(b);
}

这是我的错误

error C4430: Missing type specifier - int assumed . Note: C++ does not support default-  int

我的编译器(MSVS 11)告诉我我在 AddInFront 函数的末尾有一个 C4430 错误,最后我的意思是说除了最后的花括号之外的任何内容都有错误。我已经尝试了所有事情尝试摆脱这个错误,但我似乎无法修复它。

4

1 回答 1

1

您忘记在AddInFront()函数定义中指定返回类型:

template <class T> void List<T>::AddInFront(Node<T> a) {
//                 ^^^^
//                 This was missing
    a.chainNode(Head);
    Head = nullptr;
    Head = &a;
}

另请注意,复制初始化如下:

Node<int> a = Node<int>(20);
List<int> d = List<int>(a);
Node<int> b = Node<int>(20);

是不必要的。而是使用直接初始化:

Node<int> a(20);
List<int> d(a);
Node<int> b(20);
于 2013-05-18T18:03:09.043 回答