c++ - 需要结构包含/实现帮助 (c++)

标签 c++ struct header typedef

我正在尝试修改一些代码,以便在我的 mysh.cpp 文件中包含和使用双向链表,我得到了

error: aggregate ‘linked_list list’ has incomplete type and cannot be defined

编译。 readcommand.cpp 编译得很好,所以我只是想找出需要在头文件或 cpp 文件中更改哪些内容才能使其在 mysh 中顺利运行。

以下是所用文件的相关部分:

mysh.cpp

#include "readcommand.h"

using namespace std;

int main (int argc, char** argv) {
  readcommand read;
  linked_list list; // THIS is the line that's causing the error

  ...
}

readcommand.h

#ifndef READCOMMAND_H
#define READCOMMAND_H

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>

class readcommand {

  public:

  // Struct Definitions
  typedef struct node node_t;
  typedef struct linked_list linked_list_t;

  struct node;
  struct linked_list;

...
};

#endif

读取命令.cpp

#include "readcommand.h"

using namespace std;

struct node {
  const char *word;
  node *prev;
  node *next;
};

struct linked_list {
  node *first;
  node *last;
};

...

自从我在 C++ 或一般语言中使用 header 以来已经有一段时间了。我试过将有问题的行更改为

read.linked_list list;

read.linked_list list = new linked_list;

之类的,但它只是将错误更改为

error: ‘class readcommand’ has no member named ‘linked_list’

error: invalid use of ‘struct readcommand::linked_list’

提前致谢。

最佳答案

你需要把这些...

struct node {
  const char *word;
  node *prev;
  node *next;
};

struct linked_list {
  node *first;
  node *last;
};

...在 它们用于 class readcommand 之前,编译器会在某个地方看到它们。可能最简单的做法是将它们放在 readcommand.h before class readcommand 中。问题是 nodelinked_list 正在你的 class readcommand 中使用,但编译器不知道它们在那个时候是什么编译。

关于c++ - 需要结构包含/实现帮助 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15049632/

相关文章:

c++ - ODBC 连接未打开

python - C++ 中等效的 opencv python 代码

c++ - 如何使用内存,如直接访问文件 : analogues of fread and fwrite functions?

c - Typedef 结构返回的值与 C 中存储的值不同

c - 指向结构的指针在传递给函数时根本不改变

html - 调整浏览器大小时更改页眉的彩色背景高度

C - 如何将自己的标题保存在单独的文件夹中

c - 如何在不同的头文件中具有相同类型和名称的两个结构而不发生冲突?

c++ - boost 互斥锁的作用域解锁

struct - Fortran - 可分配派生类型的可分配数组