C 将列表传输到类
我有一个检查点列表,然后运行一个函数。我最初在那个函数中构建了这个列表,但现在我不得不在外部构建它。问题是我不能在实现该函数的类中包含C 将列表传输到类,c,struct,C,Struct,我有一个检查点列表,然后运行一个函数。我最初在那个函数中构建了这个列表,但现在我不得不在外部构建它。问题是我不能在实现该函数的类中包含checkpoint.h,因为checkpoint.h返回该类类型的结构。初始列表在class.c中全局声明。如何将在外部创建的列表转移到类中以便使用它 我有一个标题,图灵机器.h: #ifndef __TURING_MACHINE__ #define __TURING_MACHINE__ #include "tape.h" #include "alphabe
checkpoint.h
,因为checkpoint.h
返回该类类型的结构。初始列表在class.c
中全局声明。如何将在外部创建的列表转移到类中以便使用它
我有一个标题,图灵机器.h
:
#ifndef __TURING_MACHINE__
#define __TURING_MACHINE__
#include "tape.h"
#include "alphabet.h"
#include "symbol_table.h"
...
#endif
#ifndef TURING_MACHINE_H_INCLUDED
#define TURING_MACHINE_H_INCLUDED
#include "tape.h"
#include "alphabet.h"
#include "symbol_table.h"
typedef struct checkpoint_list checkpoint_list;
typedef struct turing_machine
{
...
} turing_machine;
extern checkpoint_list *tm_function(turing_machine *);
extern turing_machine *tm_create(const char *);
#endif
以及定义检查点列表的checkpoint.h
标题类:
#ifndef __CHECKPOINT_H__
#define __CHECKPOINT_H__
#include "turing_machine.h"
...
#endif
所以我想从turing\u machine.h
向函数发送一个结构列表checkpoint
,但我不能修改任何内容,因为类必须保持这种状态
我还有图灵机器.c
:
#include "turing_machine.h"
#include "checkpoint.h"
#include "symbol_table.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
checkpoint_list *c;
#包括“turing_machine.h”
#包括“checkpoint.h”
#包括“symbol_table.h”
#包括
#包括
#包括
检查点清单*c;
所以一开始我在图灵机器中创建了这个列表,c
,但现在我必须在外部创建它,我必须初始化这个列表,但我不知道如何初始化。我希望这更清楚
我把班级这个词用错了;我只有
.c
和.h
文件。从字里行间看,我认为你的问题在于你有“相互引用”的结构
解决此问题的方法是使用不完整的类型定义:
typedef struct checkpoint_list checkpoint_list;
然后,您可以在图灵机器中使用它。h:
#ifndef __TURING_MACHINE__
#define __TURING_MACHINE__
#include "tape.h"
#include "alphabet.h"
#include "symbol_table.h"
...
#endif
#ifndef TURING_MACHINE_H_INCLUDED
#define TURING_MACHINE_H_INCLUDED
#include "tape.h"
#include "alphabet.h"
#include "symbol_table.h"
typedef struct checkpoint_list checkpoint_list;
typedef struct turing_machine
{
...
} turing_machine;
extern checkpoint_list *tm_function(turing_machine *);
extern turing_machine *tm_create(const char *);
#endif
在checkpoint.h中,您可以编写:
#ifndef CHECKPOINT_H_INCLUDED
#define CHECKPOINT_H_INCLUDED
#include "turing_machine.h"
/* No typedef here in checkpoint.h */
struct checkpoint_list
{
...
};
extern checkpoint_list *cp_function(const char *);
extern turing_machine *cp_machine(checkpoint_list *);
#endif
该技术由C标准(C90,更不用说C99或C11)识别和定义
请注意,我还重命名了include-guards;以双下划线开头的名称是为“实现”(即C编译器及其库)保留的,您不应该在自己的代码中发明和使用这些名称。这在当前毫无意义。你说“checkpoint.h返回一个结构”是什么意思?你应该发布一些有代表性的代码,而不是试图描述你的代码。此外,如果你之前的问题能够充分回答你的问题,你应该接受这些问题的一些答案。用C实现类一直都很困难。。。幸运的是,有一个新的东西在那里被称为C++…你试过了吗?看起来很有希望!