簡體   English   中英

從C ++中的另一個文件指向結構內部的結構的指針

[英]pointer to a struct inside of a struct from another file in c++

我需要創建一個像堆棧一樣的程序。 我正確地完成了所有功能。 問題是我在兩個文件中有兩個結構,但是當我嘗試將指針指向另一個結構時,它不會讓我。

第一個結構在文件“ 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

但是當我嘗試編譯它時,出現了這個錯誤:

  1. 錯誤C2143:語法錯誤:缺少';' 在“ *”之前。
  2. 錯誤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預處理程序來處理包含以下行的文件:

#include "stack.h"
#include "linkedList.h"

您將看到為什么#include從“ linkedList.h”中包括“ stack.h”,反之亦然以獲得定義的麻煩。

我試過了

cpp test.cc

test.cc的內容:

#include "stack.h"
#include "linkedList.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

從預處理器的輸出中可以看到, myStack之前未聲明myStack

void push(myStack *s, int element);

這就解釋了為什么在嘗試編譯test.cc編譯器將報告錯誤。

如果切換test.cc的行,您將獲得不同的輸出。 這將導致element在聲明可用之前被使用。

解決該問題的方法:

  1. 請勿#include “ stackedList.h”中的“ stack.h”或“ stack.h”中的“ linkedList.h”
  2. 在兩個文件中使用前向聲明以使用類型。

更新的文件:

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的定義后,“linkedList.h”“stack.h” elements ,並確保你#include僅在編譯單元“linkedList.h”。 如果您#include只是“stack.h”或#include前“stack.h” #include ING“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"

您將遇到編譯器錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM