gpt4 book ai didi

c++ - 使用模板化节点的模板化列表类中缺少类型说明符

转载 作者:太空宇宙 更新时间:2023-11-04 11:54:43 25 4
gpt4 key购买 nike

这是没有标题保护和其他功能的 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 错误,到最后我的意思是它说的是除了结尾花括号之外的任何东西都有错误。我已经尝试了下面的所有东西月亮试图摆脱这个错误,但我似乎无法修复它。

最佳答案

您忘记在 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);

关于c++ - 使用模板化节点的模板化列表类中缺少类型说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16627344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com