2
我正在研究Binary Search Tree類,並且無法編碼重載流操作函數。這是我的代碼...我已經完成了我在網上找到的一切(以及我的教授的權力點),所以我不知道我做錯了什麼。STL在朋友函數中的問題
*編輯幾秒鐘前更新我的代碼更改。決定要求從目標函數被髮送到具有類似的代碼到這是在友元函數的朋友......
在頭文件的相關頭(h文件):
friend ostream& operator<<(ostream& out, const BST& tree);
void leveltraversal(ostream& out);
相關的「私人「數據/嵌套類在頭文件(h文件):
private:
class BinNode {
public:
T data;
BinNode* left;
BinNode* right;
BinNode() : left(NULL), right(NULL) {}
BinNode (T item): data(item), left(NULL), right(NULL) {}
};
typedef BinNode* BinNodePtr;
BinNodePtr myRoot;
相關實現文件的功能:
ostream& operator<<(ostream& out, const BST& tree)
{
tree.leveltraversal(out);
return out;
}
template <typename T>
void BST<T>::leveltraversal(ostream& out)
{
int level = 0;
BinNodePtr temp = myRoot;
queue<BinNodePtr> nodes;
nodes.push(temp);
out << endl << endl;
while (!nodes.empty()){
temp = nodes.front();
level = recursive_level(temp->data);
out << endl << endl;
out << "Node data: " << temp->data;
out << endl;
out << "Level: " << level;
nodes.pop();
if (temp->left)
nodes.push(temp->left);
if (temp->right)
nodes.push(temp->right);
}
}
我會發布編譯器錯誤,但他們繼續了很多行,我覺得這些問題是不言而喻的。不過,如果有人願意,他們會更新!
你可能想要包含編譯器錯誤/警告 – gvd
噢!對不起......我整天都在編碼,而且有點累。感謝您的幫助:) – user1799411
很難說沒有錯誤信息(第一個(或幾個)通常就足夠了),但首先,你是在用什麼實例化'BST'模板,即什麼是'temp - > data'? – molbdnilo