2012-04-24 50 views
0

我創建了一個類並將其分割爲源文件和頭文件,但我無法讓它們相互交談。在名稱空間/頭文件中找不到類

我的頭文件,GridLayout.h看起來是這樣的:

#ifndef GRIDLAYOUT_H_INCLUDED 
#define GRIDLAYOUT_H_INCLUDED 

#include <vector> 
#include <types.h> 
#include "GridPlaceable.h" 

namespace Spaceships { 

class GridLayout { 
    //consider replace with std::list 
    typedef std::vector<GridPlaceable*> column; 

    public: 
     GridLayout(); 
     ~GridLayout(); 

     void arrange(); 
     void splitColumn(size_t colNo, distance position); 
     void splitRow(size_t rowNo, distance position); 
     bool placeOne(GridPlaceable* thatOne); 

private: 
     bool tryToFit(GridPlaceable* thatOne, size_t startCol, size_t startCell); 

     std::vector<column> wholeGrid; 
     std::vector<GridPlaceable*> toPlace; 

     std::vector<distance> rowHeights, colWidths; 
     std::vector<size_t> firstEmpties; 

     bool mandates; 
}; 

}; 

GridLayout.cpp樣子:

#include "GridLayout.h" 

namespace Spaceships { 

GridLayout::GridLayout() { 

} 

//GridLayout::aBunchOfOtherFunctions() { } 

} 

#endif 

當我編譯,我得到GridLayout does not name a type錯誤的整體轉換。什麼可能導致這個?我似乎還記得曾經拋出一堆分號來解決類似的問題,但這次似乎並不奏效。

+1

這聽起來像你要我們幫助你的語法錯誤。在這種情況下,您需要發佈* real *代碼,否則我們最終會追逐無關緊要的問題。 – 2012-04-24 20:52:24

+0

是真碼嗎?你在類定義,構造函數聲明,命名空間聲明之後缺少';' – EdChum 2012-04-24 20:57:29

+0

@EdChum,命名空間分號是可選的。 – chris 2012-04-24 20:58:24

回答

2

想通了!我會把它放在這裏,以防有人幫忙。

我必須設置GridLayout.h在某些時候編譯,然後將其改回,因爲在目錄中有一個GridLayout.gch。所以編譯器一定是直接爲此而忽略了.h。我刪除該文件,現在它顯示我的代碼中的所有真實錯誤。

+0

恭喜修復!如果可以,請確保將您的答案標記爲「已接受」,以便其他人能夠從您的成功中學習。乾杯〜 – 2012-04-27 19:16:12

3

你的「實際頭文件」包含了默認的構造函數的兩個聲明:

public: 
    GridLayout(); 
    GridLayout(); 

您應該刪除其中之一。

編輯:現在你缺少一個#endif在頭的末端,有一個在.cpp文件的末尾太多......

+0

**從來沒有**會猜到。 – chris 2012-04-24 21:15:36

+0

哦,實際上,我在我的代碼中修復了那個,但我想我忘了在這裏修復它。我現在要做。 – 2012-04-24 21:19:37

相關問題