daj*_*jee 3 c++ tree data-structures
对于我的数据结构类,我们正在创建一个可用于轻松存储和组织数据的数据结构.我的树的输出功能有问题.我得到的错误消息是:
AccountDB.cpp: In member function ‘void AccountDB::output(std::ostream&) const’:
AccountDB.cpp:23:21: error: passing ‘const AccountDB’ as ‘this’ argument of ‘void
AccountDB::output(std::ostream&, const AccountDB::Elem*)’ discards qualifiers [-fpermissive]
Run Code Online (Sandbox Code Playgroud)
我一直在环顾四周,我的输出代码看起来与其他人的做法非常相似.我不知道,我真的不明白这个错误试图告诉我什么.
谢谢你的帮助.
标题:
#ifndef ACCOUNTDB_H
#define ACCOUNTDB_H
#include <iostream>
using namespace std;
#include "AccountRecord.h"
class AccountDB {
public:
AccountDB();
~AccountDB();
void insert( const AccountRecord &v );
AccountRecord * get( const AccountRecord &v );
void output( ostream &s ) const;
private:
struct Elem {
AccountRecord info;
Elem *left;
Elem *right;
};
Elem *root;
void insert( const AccountRecord &v, Elem *&e );
AccountRecord * get( const AccountRecord &v, Elem *&e );
void output( ostream &s, const Elem *e );
};
ostream &operator << ( ostream &s, const AccountDB &v );
#endif
Run Code Online (Sandbox Code Playgroud)
资源
#include "AccountDB.h"
//default constructor
AccountDB::AccountDB() {
root = 0;
}
//destructor
AccountDB::~AccountDB() {
}
//public
void AccountDB::insert( const AccountRecord &v ) {
return insert( v, root );
}
AccountRecord * AccountDB::get( const AccountRecord &v ) {
return get( v, root );
}
void AccountDB::output( ostream &s ) const {
output( s, root );
}
//private
void AccountDB::insert( const AccountRecord &v, Elem *&e ) {
if( e == NULL ) {
e = new Elem();
e->info = v;
}
else if( v < e->info )
insert( v, e->left );
else if( v > e->info )
insert( v, e->right );
}
AccountRecord * AccountDB::get( const AccountRecord &v, Elem *&e ){
if( e->info == v )
return &(e->info);
else if( v < e->info && e->left != NULL )
get( v, e->left );
else if( v > e->info && e->right != NULL )
get( v, e-> right );
else
return NULL;
}
void AccountDB::output( ostream &s, const Elem *e ) {
if( e != NULL ) {
output( s, e->left );
s << e->info << endl;
output( s, e->right );
}
}
ostream &operator << ( ostream &s, const AccountDB &v ) {
v.output( s );
return s;
}
Run Code Online (Sandbox Code Playgroud)
您的output功能未声明const,因此当您致电时
output( s, root );
Run Code Online (Sandbox Code Playgroud)
编译器告诉您从函数内部调用非const const函数.
有几种方法可以解决这个问题 - 一种是制造outputconst; 另一种是制造output静电(如果可以的话).