C++ 从模板转换为常量int时出现二进制搜索树错误
我正在做作业,我正在尝试实现一些二进制搜索树函数。我对编程也很陌生,没有太多编程经验。我已经写出了函数,但我正试图找出我收到的错误。我收到的错误是C++ 从模板转换为常量int时出现二进制搜索树错误,c++,binary-search-tree,C++,Binary Search Tree,我正在做作业,我正在尝试实现一些二进制搜索树函数。我对编程也很陌生,没有太多编程经验。我已经写出了函数,但我正试图找出我收到的错误。我收到的错误是btree.h(76):错误C2664:'Node::find_Node':无法将参数1从'Node*'转换为'const int&' 具有 [ T=int ] 原因:无法从“Node*”转换为“const int” 具有 [ T=int ] 这就是为什么我也放cpp文件的原因。在cpp文件中也获取一个错误。所有的档案都是我的教授提供的。我只需要向no
btree.h(76):错误C2664:'Node::find_Node':无法将参数1从'Node*'转换为'const int&'
具有
[
T=int
]
原因:无法从“Node*”转换为“const int”
具有
[
T=int
]
这就是为什么我也放cpp文件的原因。在cpp文件中也获取一个错误。所有的档案都是我的教授提供的。我只需要向node.h添加两个方法,就是这样。这是cpp文件tree2.cpp中的错误
tree2.cpp(28):请参阅对正在编译的类模板实例化“BinaryTree”的引用
具有
[
elemType=int
]
如果有人能帮助解释如何用建议来修复错误,那就太好了
btree.h
#include <iostream>
#include "node.h"
//using namespace std;
template < typename elemType >
class BinaryTree {
public:
BinaryTree( );
~BinaryTree( );
void insert( const elemType & );
void remove( const elemType & );
void inorder( );
bool empty( );
void clear( );
private:
Node< elemType > *_root;
BinaryTree( const BinaryTree & );
BinaryTree &operator =( const BinaryTree & );
void clear( Node< elemType > * );
};
template < typename elemType >
inline BinaryTree< elemType >::BinaryTree( ) : _root(0)
{
cout << "BinaryTree::BinaryTree() "
<< "default constructor\n";
}
template < typename elemType >
inline BinaryTree< elemType >::~BinaryTree( )
{
cout << "BinaryTree::~BinaryTree() destructor\n";
clear( );
}
template < typename elemType >
inline void BinaryTree< elemType >::clear( )
{
if( _root )
{
clear( _root );
_root = 0;
}
}
template < typename elemType >
inline void BinaryTree< elemType >::clear( Node< elemType > *pt )
{
if( pt ) {
cout << "clear( ) left of " << pt->value( ) << '\n';
clear( pt->left( ) );
cout << "clear( ) right of " << pt->value( ) << '\n';
clear( pt->right( ) );
delete pt;
}
}
template < typename elemType >
inline void BinaryTree< elemType >::insert( const elemType &e )
{
if( !_root )
{
_root = new Node< elemType >( e );
}
else
{
_root->insert_value( e );
}
}
template < typename elemType >
inline void BinaryTree< elemType >::remove( const elemType &e )
{
_root->find_node( _root, e );
}
template < typename elemType>
inline void BinaryTree< elemType >::inorder( )
{
_root->inorder( _root );
cout << '\n';
}
tree2.cpp
#include "btree.h"
#include <fstream>
#include <string>
using namespace std;
ifstream get_ifs( ) // get input file stream
{
string filename; // input file name
cerr << "name of file to read from? ";
cin >> filename;
ifstream ifs( filename, ifstream::in );
if( ! ifs ) // cannot open file infilen
{
cerr << "cannot open input file '" << filename << "'\n";
exit( 1 );
}
return ifs; // return input file stream
}
int main()
{
BinaryTree< int > bt;
bt.insert( 30 );
bt.insert( 20 );
bt.insert( 40 );
bt.insert( 50 );
bt.insert( 10 );
bt.insert( 15 );
bt.insert( 35 );
cout << "inorder traversal:\n";
bt.inorder( );
ifstream ifs = get_ifs( );
string cmd;
int value;
while( ifs >> cmd )
{
ifs >> value;
cout << cmd << '\t' << value << '\n';
if( cmd == "a" )
{
bt.insert( value );
}
else if( cmd == "d" )
{
bt.remove( value );
}
else
{
cout << "invalid command '" << cmd << "' ignored...\n\n";
}
bt.inorder( );
cout << '\n';
}
return 0;
}
#include "btree.h"
#include <fstream>
#include <string>
using namespace std;
ifstream get_ifs( ) // get input file stream
{
string filename; // input file name
cerr << "name of file to read from? ";
cin >> filename;
ifstream ifs( filename, ifstream::in );
if( ! ifs ) // cannot open file infilen
{
cerr << "cannot open input file '" << filename << "'\n";
exit( 1 );
}
return ifs; // return input file stream
}
int main()
{
BinaryTree< string > bt;
bt.insert( "Florida" );
bt.insert( "California" );
bt.insert( "Alabama" );
bt.insert( "Mississippi" );
bt.insert( "Georgia" );
bt.insert( "Colorado" );
bt.insert( "Idaho" );
bt.insert( "Missouri" );
bt.insert( "New York" );
cout << "inorder traversal:\n";
bt.inorder( );
ifstream ifs = get_ifs( );
string cmd, value;
while( ifs >> cmd )
{
ifs >> value;
cout << cmd << '\t' << value << '\n';
if( cmd == "a" )
{
bt.insert( value );
}
else if( cmd == "d" )
{
bt.remove( value );
}
else
{
cout << "invalid command '" << cmd << "' ignored...\n\n";
}
bt.inorder( );
cout << '\n';
}
return 0;
}
是否从更改以下行
inline void find_node( const T & val, const T * root ) {
到
帮忙
另请参见:find\u node方法希望
T
类型不是节点;它需要节点中的数据类型
当它不需要时,您正在将其传递给整个节点 你好,鲍勃,欢迎致电StackOverflow。如果您想让我们帮助您编写代码,您应该养成发布最少示例的习惯。这意味着将代码缩减到编译并产生错误的最小示例。这也是一项很好的技能,可以学习供自己使用。@Beta很抱歉,我的代码没有编译。这就是我试图修复的,以便它能够编译。我收到了这个错误,并试图找出如何解决它,真是太粗心了。我在本例中所指的是产生该错误的最小示例,而不是其他错误。我理解。我之所以提供所有文件,是因为cpp文件是我的变量value被指定为int数据类型的地方。我认为将cpp文件包括为wellBob会很有帮助,顺便问一下,这其中有多少是您的实现?只是提供了标题声明,而您必须提供模板成员体吗?我已经尝试过了。但是老师说我们需要两个论点。教授希望我们更改的唯一文件是node.hfile@bob:我现在明白了!我已经更新了答案,请看这是否有意义。我对这个很陌生。如何将其传递给整个节点?模板
inline void find_node( const T & val, const T * root ) {
inline void find_node( const T * root, const T & val ) {