我正在尝试为我的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


readcommand.cpp

#include "readcommand.h"

using namespace std;

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

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

...




自从我在c ++或一般语言中使用标头以来已经有一段时间了。我尝试将相关行更改为

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中使用它们之前,编译器将在这些位置看到它们的地方。可能最简单的方法是将它们放在class readcommand之前的readcommand.h中。问题是在您的node中使用了linked_listclass readcommand,但是编译器当时不知道它们在编译时的含义。

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

10-13 08:26