簡體   English   中英

struct的前向聲明無效

[英]invalid forward declaration of struct

--EDIT - 很抱歉我混淆了人們,我只是快速輸入這個代碼而不是復制和粘貼,所以我實際上在我的代碼中執行#ifndef A_H #define A_H。 我已經改變了下面的代碼來表明這一點
- 結束編輯 -

我有兩個類,每個類都包含一個指向另一個類的實例的指針,但這給我帶來了問題。 我的代碼類似於以下內容

// A.h
#ifndef A_H
#define A_H

class B; // compiler error here

class A
{
  B* foo;
  // other members and functions
};

#endif

// A.cpp
#include "A.h"
#include "B.h"
/*
 declare functions and use methods in both A and B
*/

// B.h
#ifndef B_H
#define B_H

class A;

class B
{
  A** bar;
// other stuff
};

#endif

//B.cpp
#include "A.h"
#include "B.h" 
/*
declare functions and use methods in both A and B
*/

有人告訴我前面聲明另一個類int頭文件然后包含cpp文件中的另一個文件會起作用,但是在標記行上我得到一個錯誤,只是說“'b'的前向聲明”

誰能告訴我我做錯了什么?

包括一個標題,讓我說bh中啊不要轉發聲明B在ahbh中可以保持原樣。

否則你會得到......

class B {};
....
class B;

僅對這些錯誤進行預處理總是明智的。

暫無
暫無

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

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