结构包含/实现帮助需要(c ++) [英] Struct Inclusion/Implementation Help Needed (c++)

查看:120
本文介绍了结构包含/实现帮助需要(c ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想适应一个双重链表的代码,以包含和使用我的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 before class readcommand. The problem is that node and linked_list are being used in your class readcommand but the compiler doesn't know what they are at that point in the compilation.

这篇关于结构包含/实现帮助需要(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆