Пишу дерево с любым количеством наследников. В классе Thread содержится вектор классов Thread. При попытке просмотра первого элемента в векторе, нет доступа к методам класса. Подскажите пожалуйста, что нужно исправить чтобы получить доступ к методам?
| Код | ... class Thread { private: int *key=nullptr; vector<Thread> *left=nullptr; ... void show_ar_L(){cout<<left[0]<<endl;} // <-- Методов класса нет
|
Советовали сделать так, но не выходит( вылетает ошибка)
| Код | void show_ar_L(){cout<<(*left)[0].getX()<<endl;}
|
Ниже прикладываю код класса и еще ниже код всей программы
| Код | class Thread { private: int *key=nullptr; vector<Thread> *left=nullptr; vector<Thread> *right=nullptr; public: Thread(){key=new int(-1);left=new vector<Thread>;right==new vector<Thread>;} Thread(const int &x){key=new int(x);left=new vector<Thread>;right==new vector<Thread>;} Thread(const Thread *t){key=t->key;left=t->left;right=t->right;} void show_ar_L(){cout<<left[0]<<endl;}//cout<<<<endl;} int getX()const{return *key;} int & getX(){return *key;} void add(Thread *t){Thread *temp=new Thread(t);t->left->push_back(temp);}//delete temp operator=(Thread *t){key=t->key;left=t->left;right=t->right;} friend ostream & operator<<(ostream & os, const Thread * t){os<<t->getX();return os;} };
|
| Код | #include <iostream> #include <stack> #include <fstream> #include <map> #include <memory> #include <vector> #include <algorithm> using namespace std; void showmap(const multimap<int,int> &a); class Thread { private: int *key=nullptr; vector<Thread> *left=nullptr; vector<Thread> *right=nullptr; public: Thread(){key=new int(-1);left=new vector<Thread>;right==new vector<Thread>;} Thread(const int &x){key=new int(x);left=new vector<Thread>;right==new vector<Thread>;} //Thread(const int &x,const int &n){key=new int(x);left=new vector<Thread>(n);right==new vector<Thread>(n);} Thread(const Thread *t){key=t->key;left=t->left;right=t->right;} //void show_ar(){cout<<(void *)this<<" "<<*key<<" "<<(void *)left<<" "<<endl;} void show_ar_L(){cout<<left[0]<<endl;}//cout<<<<endl;} int getX()const{return *key;} int & getX(){return *key;} void add(Thread *t){Thread *temp=new Thread(t);t->left->push_back(temp);}//delete temp operator=(Thread *t){key=t->key;left=t->left;right=t->right;} friend ostream & operator<<(ostream & os, const Thread * t){os<<t->getX();return os;} }; int main() { fstream nfile; nfile.open("treads2.txt"); int n; nfile>>n; multimap <int,int>one; for(int i=0;i<n;++i) { int x; int y; nfile>>x; nfile>>y; one.insert(pair<int,int>(x,y)); } Thread *a = new Thread(); a->getX()=2; Thread *b = new Thread(); b->getX()=1; cout<<a->getX()<<endl; cout<<b->getX()<<endl; a->add(b); cout<<a->getX(); b->getX()=4; cout<<b; cout<<a; } void showmap(const multimap<int,int> &a) { for(auto it=a.begin();it!=a.end();++it)cout<<it->first<<" "<<it->second<<endl; }
|
|