【问题标题】:'Undeclared identifier' / 'identifier "x" undefined' / 'class Y has no member "x" ' working with struct-type pointers'未声明的标识符'/'标识符“x”未定义'/'class Y没有成员“x”'使用结构类型指针
【发布时间】:2026-02-18 14:25:03
【问题描述】:

我有我的类 Queue(在 Queue.h 中声明并在 Queue.cpp 中实现)和 Queue.h 文件中定义的“节点”结构,如下所示:

Queue.h

//...
typedef int TElem;
struct Node{
    TElem data;
    Node *next;
};
class Queue
{
private:
    Node *head;
    Node *tail;
public:
    // ... some other methods
}

然后,在 Queue.cpp

//...
Queue::Queue()
{
    head = nullptr;
    tail = nullptr;
}

我的构造函数中的最后 2 行代码产生 4 个错误,每行两个: '标识符“head”未定义'/'标识符“tail”未定义'和“'head':未声明的标识符”/“'tail':未声明的标识符”。为什么会这样,我该如何解决?我已经阅读了类似问题的答案,但没有人回答我的问题。

【问题讨论】:

    标签: class pointers struct header identifier


    【解决方案1】:

    很抱歉,我的文件有问题。创建项目时,我对文件做了一些不好的事情。之后代码运行良好。

    【讨论】: