質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

2回答

3855閲覧

二分探索木で最大値を返したい

Ryuji_T

総合スコア6

コンパイルエラー

コンパイルのフェーズで生成されるエラーです。よく無効なシンタックスやタイプが含まれているとき発生します。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2021/02/18 02:56

C++初心者です。
二分探索木の中の最大値を求めるコードを作りたいのですが、コンパイル中にcollect2というエラーが発生します。原因と解決方法がわからないのでご教授をお願いしたいと思います。
コードはファイルの一部をコピペしてきたものなので、余計な定義などが入っているかもしれませんが、気にしないでください。
よろしくお願いします。

エラーメッセージ

/tmp/ccBj1FvS.o: 関数 `main' 内: bstmax.cpp:(.text+0x2da): `BinarySearchTree::findLargest() const' に対する定義されていない参照です collect2: エラー: ld はステータス 1 で終了しました

ソースコード

C++

1#include <iostream> 2#include <vector> 3#include <cassert> 4using namespace std; 5 6// 二分木の頂点のクラス 7template <class T> class BinNode { 8private: 9 T key; // 各頂点のキー 10 BinNode<T> *left, *right; // 左子・右子頂点 11public: 12 // コンストラクタでメンバを初期化 13 BinNode(T d, BinNode<T> *l=NULL, BinNode<T> *r=NULL) { 14 key = d; left =l; right=r; 15 } 16 // BinarySearchTree のメンバ関数からこのクラス BinNode のプライベートメンバを操作可能にする 17 friend class BinarySearchTree; 18}; 19 20// 二分探索木のクラス 21class BinarySearchTree { 22 BinNode <int>* root; // 根頂点 23public: 24 BinarySearchTree( ) { root = NULL; }; // 初期状態 25 ~BinarySearchTree( ) { makeEmpty( root ); }; // デストラクタ 26 bool insert( int data ) { return insert( data, root ); }; 27 void show() const { show( root ); cout << endl; }; 28 int findLargest() const; // 29private: 30 bool insert( int data, BinNode<int>* & tree); 31 void makeEmpty( BinNode<int>* tree ); 32 void show( BinNode<int>* tree ) const; 33 int findLargest( BinNode<int>* tree ) const; 34 }; 35 36void BinarySearchTree::makeEmpty( BinNode<int>* tree ) { 37 if( tree !=NULL ) { 38 makeEmpty( tree->left ); 39 makeEmpty( tree->right ); 40 delete tree; 41 } 42} 43 44bool BinarySearchTree::insert( int data, BinNode<int>* & tree ) { 45 if (tree == NULL) { 46 tree = new BinNode<int> (data); 47 return true; 48 } 49 else if (tree->key == data) 50 return false; 51 else if (tree->key > data) 52 return insert(data, tree->left); 53 else 54 return insert(data, tree->right); 55} 56void BinarySearchTree::show( BinNode<int>* tree ) const { 57 if (tree != NULL) { 58 show( tree->left ); 59 cout << tree->key << ". "; 60 show( tree->right ); 61 } 62} 63 64// 二分木全体の最大値を求める関数 65int BinarySearchTree::findLargest( BinNode<int>* tree ) const { 66 if(tree==NULL) return false; 67 else{ 68 int max = tree->key; 69 int lmax = findLargest(tree->left); 70 int rmax = findLargest(tree->right); 71 if (lmax>rmax) max=lmax; 72 else max=rmax; 73 return max; 74 } 75} 76 77int main(void) { 78 BinarySearchTree bst; // 二分探索木 79 int data; // 現在の読み込みデータ 80 81 while ( cin >> data ) 82 bst.insert(data); 83 bst.show(); 84 85 cout << "最大値は " << bst.findLargest() << endl; 86 87 return 0; 88} 89

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

× int findLargest() const;
○ int findLargest() const { return findLargest(root); }

[ついでに]

× else max=rmax;
○ if (rmax>max) max=rmax;
※ max, rmax, lmax の最大値を求めるんだから。

[おまけ]

C++

1int BinarySearchTree::findLargest( BinNode<int>* tree ) const { 2 if(tree==NULL) return false;

treeが空のとき、false(=0)を返す。
空のときは最大値は存在しないのだからなにも返せないはず。
たとえば range_error例外をthrowせんならんのでは?
※ int* を返すことにすれば、NULL(nullptr)で"存在しない"ことを表現できるけど。

...で、僕ならこう書く:

C++

1#include <iostream> 2 3// 二分探索木のクラス 4template<typename T> 5class BinarySearchTree { 6 struct BinNode { 7 T key; // 各頂点のキー 8 BinNode* left; 9 BinNode* right; 10 BinNode(T d, BinNode *l=nullptr, BinNode *r=nullptr) : key(d), left(l), right(r) {} 11 }; 12 BinNode* root; // 根頂点 13public: 14 BinarySearchTree( ) { root = nullptr; }; // 初期状態 15 ~BinarySearchTree( ) { makeEmpty( root ); }; // デストラクタ 16 bool insert( const T& data ) { return insert( data, root ); }; 17 void show() const { show( root ); std::cout << std::endl; }; 18 bool findLargest(T& result) const { return findLargest(root, result); } 19private: 20 bool insert( const T& data, BinNode* & tree); 21 void makeEmpty( BinNode* tree ); 22 void show( BinNode* tree ) const; 23 bool findLargest( BinNode* tree, T& result) const; 24}; 25 26template<typename T> 27void BinarySearchTree<T>::makeEmpty( BinarySearchTree<T>::BinNode* tree ) { 28 if( tree !=nullptr ) { 29 makeEmpty( tree->left ); 30 makeEmpty( tree->right ); 31 delete tree; 32 } 33} 34 35template<typename T> 36bool BinarySearchTree<T>::insert( const T& data, BinarySearchTree<T>::BinNode* & tree ) { 37 if (tree == nullptr) { 38 tree = new BinNode(data); 39 return true; 40 } 41 else if (tree->key == data) 42 return false; 43 else if (tree->key > data) 44 return insert(data, tree->left); 45 else 46 return insert(data, tree->right); 47} 48 49template<typename T> 50void BinarySearchTree<T>::show( BinarySearchTree<T>::BinNode* tree ) const { 51 if (tree != nullptr) { 52 show( tree->left ); 53 std::cout << tree->key << ". "; 54 show( tree->right ); 55 } 56} 57 58// 二分木全体の最大値を求める関数 59template<typename T> 60bool BinarySearchTree<T>::findLargest( BinarySearchTree<T>::BinNode* tree, T& result) const { 61 if(tree==nullptr) return false; 62 else{ 63 result = tree->key; 64 T cmax; 65 if ( findLargest(tree->left, cmax) && cmax > result ) result = cmax; 66 if ( findLargest(tree->right, cmax) && cmax > result ) result = cmax; 67 return true; 68 } 69} 70 71int main() { 72 BinarySearchTree<int> bst; // 二分探索木 73 74 for ( int data : { 1, 3, 5, 7, 9, 0, 2, 4, 6, 8} ) { 75 std::cout << "insert: " << data << std::endl; 76 bst.insert(data); 77 bst.show(); 78 int max; 79 if ( bst.findLargest(max) ) { 80 std::cout << "最大値は " << max << std::endl << std::endl; 81 } 82 } 83}

投稿2021/02/18 03:03

編集2021/02/18 06:26
episteme

総合スコア16614

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Ryuji_T

2021/02/18 08:36

早速の回答ありがとうございます。episteme様の回答自体は理解できたのですが、「関数の宣言を変更せずに」このエラーを取り除くことは可能でしょうか。先生からそのような制限がかかっているものでして…
episteme

2021/02/18 10:06

できますよもちろん。
Ryuji_T

2021/02/18 12:45

それはどういう風に書き直せばいいのでしょうか
episteme

2021/02/18 13:08 編集

関数の宣言を変更せずに、コレ↓を追加。 int BinarySearchTree::findLargest() const { return findLargest(root); }
Ryuji_T

2021/02/20 02:19

無事エラーがなくなりました。ありがとうございました。
guest

0

エラーメッセージのとおりです。BinarySearchTree::findLargest() const(引数なしのオーバーロード)の定義がありません。

投稿2021/02/18 02:59

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Ryuji_T

2021/02/20 06:54

ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問