【发布时间】:2014-02-03 13:46:47
【问题描述】:
我正在尝试为 C++ 中的双向链表创建一个节点,但在构造函数中遇到了一些问题。我有以下简单的头文件:
class Node{
public:
Node();
private:
int data;
Node* next;
Node* previous;
};
我的 .cpp 文件如下所示:
#include <iostream>
#include <cstdlib>
using namespace std;
int data;
Node* next;
Node* previous;
Node::Node(){
data = 0;
next = NULL;
previous = NULL;
}
编译时出现以下错误:“节点未命名类型。”
我也尝试过使用 'struct' 来创建节点:
struct Node{
int data;
Node* next;
Node* previous;
}
但是,这给了我在 cpp 文件中的构造函数上的另一个错误: “隐式声明的定义......”
如何通过使用构造函数和变量使该程序编译而没有错误消息,我做错了什么?
【问题讨论】:
标签: c++ constructor doubly-linked-list