【问题标题】:Linkedlist Node in C+C++中的链表节点
【发布时间】:2009-05-28 00:59:34
【问题描述】:

我正在学习一本关于数据结构的书,并在链表示例中编译了它们的节点,我收到此错误:

 and Everything.cpp|7|error: expected unqualified-id before "int"|
 and Everything.cpp|7|error: expected `)' before "int"|
||=== Build finished: 2 errors, 0 warnings ===|

节点的代码是:

typedef struct Node
{
    struct Node(int data)    //Compile suggest problem is here
    {
        this-> data = data;
        previous = NULL;
        next = NULL;
    }
    int data;
    struct Node* previous;
    struct Node* next;
} NODE;

我不熟悉结构,我正在使用 Code::blocks 进行编译。有谁知道怎么回事?

【问题讨论】:

  • 我不确定我是否信任这本 C++ 数据结构书的作者。无需使用“struct Node*”(“Node*”即可),或将 Node 类型定义为 NODE。它不能与 C 兼容,因为该结构有一个构造函数。奇怪的。也许它已经很老了。

标签: c++ linked-list


【解决方案1】:

代码示例错误。构造函数声明前面不应该有关键字struct。应该是:

typedef struct Node
{
    Node(int data)  // No 'struct' here
    {
        this-> data = data;
        previous = NULL;
        next = NULL;
    }
    int data;
    struct Node* previous;
    struct Node* next;
} NODE;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-25
    • 2021-10-11
    • 2016-01-02
    • 2021-07-06
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多