问题描述
我想适应一个双重链表的代码,以包含和使用我的mysh.cpp文件,我得到
错误:aggregate'linked_list list'具有不完整的类型,无法定义
编译。 readcommand.cpp编译很好,所以我只是想弄清楚需要改变的头文件或cpp文件中,以使其运行顺利为mysh。
这里是使用的文件的相关部分:
mysh.cpp
#includereadcommand.h
using namespace std;
int main(int argc,char ** argv){
readcommand read;
linked_list list; //这是导致错误的行
...
}
readcommand.h
#ifndef READCOMMAND_H
#define READCOMMAND_H
#include< cstdio>
#include< iostream>
#include< cstring>
#include< cstdlib>
class readcommand {
public:
//结构定义
typedef struct node node_t;
typedef struct linked_list linked_list_t;
struct node;
struct linked_list;
...
};
#endif
readcommand.cpp / p>
#includereadcommand.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;
等,但它只是将错误更改为
错误:'class readcommand'没有名为'linked_list'的成员
和
错误:无效使用'struct readcommand :: linked_list'
提前感谢。
解决方案你需要把这些...
struct node {
const char * word;
node * prev;
node * next;
};
struct linked_list {
node * first;
node * last;
};
...编译器将在之前查看它们在
class readcommand
中。可能最简单的做法是将它们放在readcommand.h 之前class readcommand
。问题是在类读取命令中使用
节点
和linked_list
$ c>,但编译器不知道它们在编译的那一点。
I'm trying to adapt a bit of code for a doubly-linked list to be included and used in my mysh.cpp file, and I'm getting
error: aggregate ‘linked_list list’ has incomplete type and cannot be defined
on compile. readcommand.cpp compiles just fine, so I'm just trying to figure out what needs to be changed either in the header file or cpp files to get it running smoothly for mysh.
Here are the relevant portions of the files used:
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; }; ...
It's been a while since I've used headers in c++, or the language in general. I've tried changing the line in question to
read.linked_list list;
and
read.linked_list list = new linked_list;
and the like, but it just changes the error to things like
error: ‘class readcommand’ has no member named ‘linked_list’
and
error: invalid use of ‘struct readcommand::linked_list’
Thanks ahead of time.
解决方案You need to put these...
struct node { const char *word; node *prev; node *next; }; struct linked_list { node *first; node *last; };
...someplace where the compiler will see them before they are used in
class readcommand
. Probably the easiest thing to do would be to put them in readcommand.h beforeclass readcommand
. The problem is thatnode
andlinked_list
are being used in yourclass readcommand
but the compiler doesn't know what they are at that point in the compilation.这篇关于结构包含/实现帮助需要(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!