我正在尝试修改一些代码,以便在我的 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
中。问题是 node
和 linked_list
正在你的 class readcommand
中使用,但编译器不知道它们在那个时候是什么编译。
关于c++ - 需要结构包含/实现帮助 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15049632/