2014-10-23 4 views
0

스택처럼 작동하는 프로그램을 만들어야합니다. 나는 모든 기능을 올바르게했다. 문제는 두 파일에 두 개의 구조체가 있지만 다른 구조체에 포인터를 놓으려고 할 때이를 허용하지 않기 때문입니다. 두 번째 구조체가 두 번째 파일 "stack.h '에 선언되어있다C++의 다른 파일에서 구조체 내부의 구조체에 대한 포인터

#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 

:

첫 번째 구조체는 파일"linkedList.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 

을하지만 때 그것을 컴파일하려고하면 내게이 오류가 발생합니다 :

  1. 오류 C2143 : 구문 오류 : 누락 된 ';' '*'앞에.
  2. 오류 C4430 : 누락 된 형식 지정자 - int가 사용됩니다. 참고 :

    elements* firstElement; 
    

    방법이 문제를 해결합니까 : C++ 기본-INT

다시 오류 지점이 코드 라인을 지원하지 않습니다?

+2

C++에서는'typedef struct elements elements; '와 같은 명령문은 필요하지 않습니다. –

+0

'struct elements * firstElement;을 시도 했습니까? – jpo38

+0

'push.h'와 'pop'을 포함한 모든 스택 연산이 속해있는'stack.h '에 선언하십시오. – molbdnilo

답변

2

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 

는 그 문제를 해결하기 위해 실제로 많은 방법이있다.

+0

포워드 선언이나 세 번째 파일을 사용하지 않는 솔루션이 필요합니다. – etamar211

+0

그 중 하나가 없으면 문제를 해결할 수 없습니다. 를 제외하고, moldbnilo에 의해 제안 된 경우 push와 pop을 stack.h로 옮기는 것을 제외하면 – jpo38

0

당신이 라인을 가진 파일을 처리하는 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이 사용되기 전에 사용됩니다.

  1. 는 "stack.h"에서 "linkedList.h"또는 "linkedList.h"
  2. 사용 앞으로 선언에 있지 #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" 

컴파일러 오류가 발생합니다.

+0

나는 forward 선언문을 사용하지 않는 해결책이 필요하다. – etamar211

관련 문제