我想在我的代碼中創建一個循環結構方法來遍歷二叉搜索樹。但是我在編譯時遇到錯誤,並且不確定原因。在C++中聲明一個struct方法
我在.h文件
和
Node* BSTree::findNode(const Node* current, const Object &target){
if(*current->item == target)
return current;
Node* temp = NULL;
if(current->nodeLeft != NULL){
temp = findNode(current->nodeLeft, target);
if(temp != NULL)
return temp;
}
if(current->nodeRight != NULL){
temp = findNode(current->nodeRight, target);
if(temp != NULL)
return temp;
}
return NULL;
}
在CPP
的私人部分得到Node* findNode(const Node *, const Object &);
。
我產生了以下錯誤:
- 錯誤C2143:語法錯誤:缺少 ';'之前'*'
-error C4430:缺少類型說明符 - 假定爲int。注意:C++不支持default-int
-error C4430:缺少類型說明符 - 假定爲int。注意:C++不支持default-int
-error C2556:'int * BSTree :: findNode(const BSTree :: Node *,const Object &)':重載函數只與返回類型'BSTree :: Node * BSTree :: findNode(const BSTree :: Node *,const Object &)'
編譯器錯誤都指向cpp中代碼的第一行。我嘗試查找錯誤代碼,但我沒有找到任何回答我的問題的原因。
什麼是導致錯誤,爲什麼我的編譯器在'int BSTree'而不是Node * BSTree?我是否犯了語法錯誤或忘記了包含?目前我只包含了iostream和fstream。
提前感謝任何花時間閱讀本文的人。
編輯:
回答科林的問題。
我的#include 「BSTree.h」 在我的.cpp
而在.H我:
#ifndef BSTREE_H
#define BSTREE_H
#include <iostream>
#include <fstream>
包括你的iostream和fstream的,但你包含.h文件? – Colin 2010-10-17 00:17:42
此外,它聽起來像編譯器無法找到節點的定義 - 請確保將.h文件也包含在.h文件中 – Colin 2010-10-17 00:18:39
請使用「1010101」按鈕進行格式化,而不是將縮進添加到第一個和第最後一行。 – Potatoswatter 2010-10-17 00:19:10