그래서 저는 바이너리 검색 트리와 해당 색인을 생성해야하는 컴퓨터 과학 수업 프로젝트를 진행하고있었습니다. 이 프로젝트에서 재귀를 사용해야했습니다.멤버 함수가 내 개체 데이터 멤버를 변경하지 않는 이유는 무엇입니까?
여기 내 클래스의 구현입니다 :
class Leaf;
struct indexEntries;
class BinarySearchTree{
public:
BinarySearchTree();
~BinarySearchTree();
//Helper Functions for recursive calls
std::string search(std::string);
void BuildTree(std::string);
void inOrderPrint();
private:
//Recursive Functions
void BuildTreeR(int start, int end, Leaf * r);
void inOrderPrint(Leaf * start);
Leaf * search(std::string inquiry, Leaf * start);
void DeallocateTree(Leaf * start);
//Data members
Leaf * root;
std::vector<indexEntries> BSTindex;
};
class Leaf{
public:
Leaf(){
indexID = "";
AccNum = "";
left = NULL;
right = NULL;
};
void set_index(std::string input) {indexID = input;};
void set_AccNum(std::string input) {AccNum = input;};
void set_left(Leaf* newLeft) {left = newLeft;};
void set_right(Leaf* newRight) {right = newRight;};
std::string get_index() {return indexID;};
std::string get_AccNum() {return AccNum;};
Leaf * get_left() {return left;};
Leaf * get_right() {return right;};
private:
std::string indexID;
std::string AccNum;
Leaf * left;
Leaf * right;
};
을 내가 루트가 가리키고있는 기능 무효 BinarySearchTree::BuildTreeR(int, int, Leaf*)
리프에 Leaf * BinarySearchTree::root
을 통과 할 때 변경되지 간다. 여기
void BinarySearchTree::BuildTreeR(int start, int end, Leaf * parent){
int mid = (start+end)/2;
if(parent == NULL){
parent = new Leaf;
parent->set_index((BSTindex[mid]).indexID);
std::string fullEntry = BSTindex[mid].dataBaseEntry;
parent->set_AccNum(fullEntry.substr(4, 3));
}
if((mid-1)>start){
BuildTreeR(start, mid-1, parent->get_left());
}
if((mid+1)<end){
BuildTreeR(mid+1, end, parent->get_right());
}
}
디버거를 사용하여, 나는이 중지되는 잎, * 부모가 변경됩니다 만, 이러한 변화는 Leaf * BinarySearchTree::root
이월되지 않습니다 리프가 가리키는 것을 발견 내 프로그램을 일하는 것.
디버거/그것을 해결하는 방법을 알고 내가 변화하기 위해 노력하고있어 데이터의 값이
CXX0030: Error: expression cannot be evaluated
이 전에 발생했던 사람을 가지고 있음을 말한다?
나는 어젯밤 프로젝트를 시작하기 전에 그걸 시도했다. 구현 및 헤더 파일에서 편집하지만 변경하려는 모든 데이터에 대해 동일한 오류가 발생했습니다. – mcnnowak
나는 이것을 다시 사용하려고 시도했지만'BinarySearchTree :: BuildTreeR '을 컴파일하면 안된다 : 매개 변수 3을'Leaf * '에서'Leaf * '로 변환 할 수 없다. – mcnnowak
@mcnnowak : 또한 get_left() '및'get_right()'를 사용하여 포인터 (또는 포인터에 대한 포인터)에 대한 참조를 반환합니다. – NPE