我需要创建一个像堆栈一样运行的程序。 我正确地完成了所有功能。 问题是我在两个文件中有两个结构,但是当我试图将指针指向另一个结构时,它不允许我这样做。
第一个结构在文件“linkedList.h”中声明:
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
#include "stack.h"
struct elements{
int element;
elements* pNext;
};
typedef struct elements elements;
void push(myStack *s, int element); // insert element to top of the stack
int pop(myStack *s); //remove element from top of the stack
#endif
第二个结构在第二个文件“stack.h”中声明:
#ifndef _MYSTACK_H
#define _MYSTACK_H
#include "linkedList.h"
struct myStack{
int maxSize;
int count;
bool empty;
elements* firstElement; //the problem is in this line*********************
};
typedef struct myStack myStack;
void initStack(myStack *s, int size);
void cleanStack(myStack *s);
bool isEmpty(myStack *s);
bool isFull(myStack *s);
#endif
但是当我试图编译它时,它给了我这个错误:
- 错误 C2143:语法错误:缺少“;”在“*”之前。
- 错误 C4430:缺少类型说明符 - 假定为 int。注意:C++不支持default-int
再次错误指向这行代码:
elements* firstElement;
如何解决这个问题?
最佳答案
linkedList.h 包含 stack.h 而 stack.h 包含 linkedlists.h....你不能那样做。然后,你的#ifndef _LINKEDLIST_H
/#define _LINKEDLIST_H
使得元素最终没有被定义...
您需要使用前向声明来移除循环依赖,如下所示。
通过以下方式更改 linkedlist.h:
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
//#include "stack.h"
struct myStack;
struct elements{
int element;
elements* pNext;
};
typedef struct elements elements;
void push(myStack *s, int element); // insert element to top of the stack
int pop(myStack *s); //remove element from top of the stack
#endif
您还可以创建包含所有函数定义的第三个文件,实际上有很多方法可以解决这个问题。
关于c++ - 指向来自 C++ 中另一个文件的结构内部结构的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26532845/