당신이 라인을 가진 파일을 처리하는 C 프리 프로세서를 사용하는 경우 :
#include "stack.h"
#include "linkedList.h"
당신은 왜 #include
이 얻을 "linkedList.h"에서 "stack.h"반대의 경우도 마찬가지을 보내고 볼 수 있습니다 정의가 까다 롭습니다.
# 1 "test.cc"
# 1 "<command-line>"
# 1 "test.cc"
# 1 "stack.h" 1
# 1 "linkedList.h" 1
# 1 "stack.h" 1
# 4 "linkedList.h" 2
struct elements{
int element;
elements* pNext;
};
typedef struct elements elements;
void push(myStack *s, int element);
int pop(myStack *s);
# 4 "stack.h" 2
struct myStack{
int maxSize;
int count;
bool empty;
elements* firstElement;
};
typedef struct myStack myStack;
void initStack(myStack *s, int size);
void cleanStack(myStack *s);
bool isEmpty(myStack *s);
bool isFull(myStack *s);
# 2 "test.cc" 2
당신이 처리기의 출력에서 볼 수 있듯이이 내가 무엇을 얻을
#include "stack.h"
#include "linkedList.h"
:
나는 test.cc의
cpp test.cc
내용을 시도 , myStack
은 줄 앞에 선언되어 있지 않습니다.
void push(myStack *s, int element);
이렇게하면 test.cc
을 컴파일하려고하면 컴파일러에서 오류를보고하는 이유가 설명됩니다.
회선을 test.cc
으로 전환하면 출력이 달라집니다. 그 결과는 element
이 사용되기 전에 사용됩니다.
- 는 "stack.h"에서 "linkedList.h"또는 "linkedList.h"
- 사용 앞으로 선언에 있지
#include
"stack.h"를 수행하는 문제를 해결 방법 두 파일 모두 해당 유형을 사용합니다.
업데이트 된 파일 :
linkedList.h :
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
struct elements{
int element;
elements* pNext;
};
typedef struct elements elements;
struct myStack; // Use forward declaration of myStack.
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
struct elements; // Use forward declaration of elements.
struct myStack{
int maxSize;
int count;
bool empty;
elements* firstElement;
};
typedef struct myStack myStack;
void initStack(myStack *s, int size);
void cleanStack(myStack *s);
bool isEmpty(myStack *s);
bool isFull(myStack *s);
#endif
업데이트, 응답 의견을 OP에서
수 있습니다. #include
"linked.h"의 stack.h를 elements
의 정의 뒤에 넣고 #include
"linkedList.h"를 컴파일 단위로 만 사용해야합니다. #include
#include
#include
"stack.h"전에 #include
"linkedList.h"를 작성하기 전에 컴파일러 오류가 발생합니다.
다음은 작동하는 전달 선언이없는 업데이트 된 파일입니다.
stack.h :
#ifndef _MYSTACK_H
#define _MYSTACK_H
struct myStack{
int maxSize;
int count;
bool empty;
elements* firstElement;
};
typedef struct myStack myStack;
void initStack(myStack *s, int size);
void cleanStack(myStack *s);
bool isEmpty(myStack *s);
bool isFull(myStack *s);
#endif
linkedList.h :
#ifndef _LINKEDLIST_H
#define _LINKEDLIST_H
struct elements{
int element;
elements* pNext;
};
typedef struct elements elements;
// myStack can see the definition of elements
#include "stack.h" // Now myStack available for use
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
test.cc :
#include "linkedList.h"
그러나, 당신이 사용하는 경우 :
test.cc :
#include "stack.h"
컴파일러 오류가 발생합니다.
C++에서는'typedef struct elements elements; '와 같은 명령문은 필요하지 않습니다. –
'struct elements * firstElement;을 시도 했습니까? – jpo38
'push.h'와 'pop'을 포함한 모든 스택 연산이 속해있는'stack.h '에 선언하십시오. – molbdnilo