我很久沒碰過C++,並且在遇到這個問題時試圖製作紅黑樹模板。我想添加一個元素,只是關閉的事情。但是當我切換功能時,Im不能有相同的RBT變量。它變成了一個全新的變量。爲什麼這不工作C++無法連接兩個對象
#include <iostream>
#include <string>
#include "redblacktree.h"
using namespace std;
int menu();
void addElement(RedBlackTree<int> RBT);
void peak(RedBlackTree<int> RBT);
int main()
{
int option=-1;
RedBlackTree<int> RBT;
while(option != 99)
{
option=menu();
switch(option)
{
case 1:
addElement(RBT);
break;
case 3:
peak(RBT);
break;
}
}
return 0;
}
int menu()
{
int option;
cout<<"1. Add element"<<endl;
cout<<"3. Peek"<<endl;
cin>>option;
cin.ignore();
return option;
}
void addElement(RedBlackTree<int> RBT)
{
int value;
cout<<"Enter element: "<<endl;
cin>>value;
cin.ignore();
RBT.insert(value);
//cout<<RBT.printRoot()<<endl; //works
}
void peak(RedBlackTree<int> RBT)
{
cout<<RBT.printRoot()<<endl; //does not work
}