2012-05-05 73 views
1

這是我的二叉樹的頭文件。 我有一個名爲TreeNode的類,當然BinaryTree類有一個指向它的根的指針。錯誤C2143:語法錯誤:缺少';'聲明指針前'*'之前

而且我得到了以下三個錯誤

error C2143: syntax error : missing ';' before '*' 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 

和代碼二叉樹的頭文件

#pragma once 

#include <fstream> 
#include <iostream> 
#include "Node.h" 
using namespace std; 

class BinaryTreeStorage 
{ 
private: 
    TreeNode* root; 

public: 
    //Constructor 
    BinaryTreeStorage(void); 

    //Gang Of Three 
    ~BinaryTreeStorage(void); 
    BinaryTreeStorage(const BinaryTreeStorage & newBinaryTreeStorage); 
    BinaryTreeStorage& operator=(const BinaryTreeStorage & rhs); 

    //Reading and writing 
    ofstream& write(ofstream& fout); 
    ifstream& read(ifstream& fin); 

}; 

//Streaming 
ofstream& operator<<(ofstream& fout, const BinaryTreeStorage& rhs); 
ifstream& operator>>(ifstream& fin, const BinaryTreeStorage& rhs); 

錯誤似乎是在第11

TreeNode* root; 

我花了幾天的努力擺脫這個錯誤並徹底毀滅。

這是關於錯誤命名空間的錯誤嗎?或者也許TreeNode類沒有宣佈正確?

而就在樹節點頭文件

#pragma once 
#include <string> 
#include "BinaryTreeStorage.h" 

using namespace std; 

class TreeNode 
{ 
private: 
    string name; 
    TreeNode* left; 
    TreeNode* right; 

public: 
    //Constructor 
    TreeNode(void); 
    TreeNode(string data); 

    //Gang of Three 
    ~TreeNode(void); 
    TreeNode(const TreeNode* copyTreeNode); 


    //Reading and writing 
    ofstream& write(ofstream& fout); 

    //Add TreeNode 
    void addTreeNode(string data); 

    //Copy tree 
    void copy(TreeNode* root); 
}; 

情況下,代碼預先感謝您。

+0

謝謝大家回答問題,這確實是一個愚蠢的問題。那就是如果你在C++之前編程C#會發生什麼。 –

回答

3

而不是

#include "Node.h" 

簡單地轉發聲明類:

class TreeNode; 

而且,你爲什麼包括Node.hBinaryTreeStorage.h?不需要它,所以刪除它。

0

你在TreeNode類的定義中使用ofstream,但你不包括此:

#include <fstream> //include this in Node.h 

請這樣做。

此外,在Node.h中不需要包含BinaryTreeStorage.h。它使事物循環。

0

正向聲明樹節點即添加它看起來class TreeNode;class BinaryTreeStorage{};

3

像Node.h被包括BinaryTreeStorage.h,所以當你嘗試編譯Node.h(類樹節點),它首先編譯BinaryTreeStorage,但需要知道什麼TreeNode還沒有被編譯。

解決這個問題的辦法是轉發類聲明:

class TreeNode; 

告訴編譯器預計將在後面定義的類類型的樹節點,但你可以在聲明該類型的指針和引用與此同時。最後要做的就是刪除#include "Node.h"。這打破了你的循環引用。

+0

TY對於循環引用的洞察力,它有助於解決我的問題! +1 – teejay

相關問題