【问题标题】:Missing type specifier On a Templated List Class using Templated Nodes使用模板化节点的模板化列表类缺少类型说明符
【发布时间】:2013-05-18 18:01:01
【问题描述】:

这是 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 错误,最后我的意思是说除了结尾大括号之外的任何内容都有错误。我已经尝试了下面的所有内容月亮试图摆脱这个错误,但我似乎无法修复它。

【问题讨论】:

  • 顺便说一句,Node&lt;int&gt; a(20);

标签: c++ templates visual-c++ data-structures compiler-errors


【解决方案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);

【讨论】:

  • 哇,我现在问这个真的很愚蠢......非常感谢
  • @MercutioCalviary:很高兴它有帮助。如果这解决了您的问题,请考虑将答案标记为已接受(何时允许)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多