2009-09-25 60 views
1

我在我的主(全局)頭文件中的類中存在一些循環聲明的情況。C++通告聲明

#include <cstdlib> 
#include <iostream> 

using namespace std; 


enum piece_t {BLACK, WHITE, EMPTY, WALL}; //wall is area out side of board (board array is 21x21 but only 19x19 is playable) 
enum dir_t {ABOVE,BELOW,LEFT, RIGHT}; //shall i overload ! or - operatior? !LEFT==RIGHT? 


struct nextPoint_t //should be implimented with references, but need to practice pointer 
{ 
    point_t* above; 
    point_t* below; 
    point_t* left; 
    point_t* right; 

}; 




class point_t 
{ 
private: 
    piece_t mType; //what sort of point this is 
    int mLiberties; 
    nextPoint_t mAdjacent; // points to adjacent points 

    bool mLibertiesCounted; // keeps track of if liberties have been counted, for mCountLiberites() (sets), is reset by mUpdateLiberites(); 

    int mCountLiberties(); //counts this point's liberites, by calling count on mAdjacent points etc. 
    void mSetPos(int xPos, int yPos, board_t theBoard); //sets up mAdjacent to point to adjacent points, 
    void mSetStructureLiberties(int numLibs); // Sets this squares liberites then calls this on all adjacent squares 


public: 
    point_t();// parameterless constructor, for arrays 
    void mSetUp(int xPos, int yPos, board_t theBoard);// sets up mType then calles setPos iFF not WALL type 
    point_t (int xPos, int yPos, board_t theBoard); //constructor, takes it's position in the grid as a parameter 

    void mUpdateLiberties(); // calles countLiberties then, updates liberites on whole of connected structure, by operating pon all conencted points 



}; 

class board_t 
{ 
private: 
    point_t mArray [21][21]; 


public: 
    board_t(); //constructor, sets up board by operating on the point_t's 

}; 

不要擔心那裏的評論,我知道我的意思。

我想我可以向聲明修復它,但他們似乎沒有工作,只是認爲我redeifining類

+0

循環聲明在哪裏?一切似乎都正確地宣告了 – dharga 2009-09-25 14:31:31

+0

哦,是point_t *在struct nextPoint_t中的問題嗎? – dharga 2009-09-25 14:33:01

+1

循環依賴總是讓我感到腥味。它看起來像你可以重構你的代碼,以避免它們。 – luke 2009-09-25 14:34:21

回答

2

在你的代碼的正向引用似乎是board_tpoint_t,這通過向前聲明它們來解決。

既然你在point_t成員函數的聲明引用board_t,你不能定義成員函數point_t的時候了。他們的定義必須在board_t定義後出現。因此,您必須將該功能的定義移動到cpp文件中,否則您必須將其定義在標頭定義爲board_t,無論哪個套件都是您的更多。 point_t只用作nextPoint_t指針對象的類型,所以我們不會有同樣的問題在這裏討論:在頭年底

class point_t; // used by nextPoint_t 
class board_t; // used by point_t 

struct nextPoint_t //should be implimented with references, but need to practice pointer 
{ 
    point_t* above; // goes without problems - doesn't need definition of point_t 
    point_t* below; 
    point_t* left; 
    point_t* right; 
}; 

class point_t 
{ 
private: 
    piece_t mType; 
    int mLiberties; 
    nextPoint_t mAdjacent; 
    bool mLibertiesCounted; 

    int mCountLiberties(); 
    void mSetPos(int xPos, int yPos, board_t theBoard); 
    void mSetStructureLiberties(int numLibs); 

public: 
    point_t(); 
    void mSetUp(int xPos, int yPos, board_t theBoard); 
    point_t (int xPos, int yPos, board_t theBoard); 

    void mUpdateLiberties(); 
}; 

class board_t 
{ 
private: 
    point_t mArray [21][21]; 

public: 
    board_t();  
}; 

定義看起來像

// define it either inline in the header, or non-inline in a cpp file 
inline void point_t::mSetPos(int xPos, int yPos, board_t theBoard) { 
    /* some work... */ 
} 

// same for mSetUp... 

不過我會建議您使用const引用將板傳遞給point_t的成員函數,但這是而不是需要您的代碼工作。聲明使用不完整的參數類型很好。

9

好吧,考慮到的意見和我自己做了測試後,真正的答案: 您不能再使用前向聲明。 :)

#include <cstdlib> 
#include <iostream> 

class point_t; 
class board_t; 

/* Rest of the code stay the same */ 
+1

你可以使用引用以及...然後你只需要改變函數,而不是所有引用它的代碼... – Goz 2009-09-25 14:31:20

+0

你是對的Goz!這樣更好:) – Jodi 2009-09-25 14:34:33

+4

在非定義函數聲明中,允許參數類型不完整。所以這應該沒有指針或引用罰款。 – 2009-09-25 14:35:04

0
#include <cstdlib> 
#include <iostream> 

enum piece_t {BLACK, WHITE, EMPTY, WALL}; //wall is area out side of board (board array is 21x21 but only 19x19 is playable) 
enum dir_t {ABOVE,BELOW,LEFT, RIGHT}; //shall i overload ! or - operatior? !LEFT==RIGHT? 

class point_t; 

struct nextPoint_t //should be implimented with references, but need to practice pointer 
{ 
    point_t* above; 
    point_t* below; 
    point_t* left; 
    point_t* right; 

}; 


class board_t; 

class point_t 
{ 
private: 
    piece_t mType; //what sort of point this is 
    int mLiberties; 
    nextPoint_t mAdjacent; // points to adjacent points 

    bool mLibertiesCounted; // keeps track of if liberties have been counted, for mCountLiberites() (sets), is reset by mUpdateLiberites(); 

    int mCountLiberties(); //counts this point's liberites, by calling count on mAdjacent points etc. 
    void mSetPos(int xPos, int yPos, const board_&t theBoard); //sets up mAdjacent to point to adjacent points, 
    void mSetStructureLiberties(int numLibs); // Sets this squares liberites then calls this on all adjacent squares 


public: 
    point_t();// parameterless constructor, for arrays 
    void mSetUp(int xPos, int yPos, const board_t& theBoard);// sets up mType then calles setPos iFF not WALL type 
    point_t (int xPos, int yPos, const board_t& theBoard); //constructor, takes it's position in the grid as a parameter 

    void mUpdateLiberties(); // calles countLiberties then, updates liberites on whole of connected structure, by operating pon all conencted points 



}; 

class board_t 
{ 
private: 
    point_t mArray [21][21]; 


public: 
    board_t(); //constructor, sets up board by operating on the point_t's 

}; 
2
如果你寫

你的結構之前

class point_t; 

應該做的伎倆。

雖然我不太確定你爲什麼要組織你的班級。您的電路板中已經有了一個陣列mArray,因此不需要在每個point_t內都有指向相鄰點的指針。

編輯:作爲另一個海報說,你需要使用指針之前。

+0

我同意,你可以很容易地找到你自己的情況下,你有不一致的數據。鄰接應該是一個計算值。加上它會節省內存;) – dharga 2009-09-25 14:52:39

+0

我怎樣才能得到不一致的數據? 只是以不同的方式訪問相同的內存。 我需要檢查多次相鄰(讀取每個函數調用的次數超過100次)重新計算會耗費時間。 我想可以把這個優化器留給我。 此外,目前沒有point_t知道它在數組中的位置;儘管這可以很好地解決。 但不會使用數組(它是board_t的一部分)是不好的做法,因爲處理point_t的所有函數都與其他point_t對象有關。 (實際上,一個point_t對象沒有意義,但它與其他人之間的關係不存在) – 2009-09-25 15:02:09

1

只需添加這上面的結構nextPoint_t

enum piece_t {BLACK, WHITE, EMPTY, WALL}; 
enum dir_t {ABOVE,BELOW,LEFT, RIGHT}; 

class point_t; 
class board_t; 

struct nextPoint_t 
{ 
    point_t* above; 
    point_t* below; 
    point_t* left; 
    point_t* right; 
}; 

,並更改爲board_t到board_t任何參考*

void mSetUp(int xPos, int yPos, board_t* theBoard);