3

for my data structures class, we are making a data structure that we can use to easily store and organize data. I am having an issue with the output function of my tree. The error message that I get is:

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]

I've been looking around and my output code looks pretty similar to what other people have done. I have no idea, and I don't really understand what the error is trying to tell.

Thanks for your help.

Header:

#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

Source

#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;
}
0

2 Answers 2

5

Your output function is not declared const, so when you call

output( s, root );

the compiler tells you that you are calling a non-const function from inside a const function.

There are several ways to deal with this - one is to make output const; the other is to make output static (if you can).

Sign up to request clarification or add additional context in comments.

6 Comments

@LuchianGrigore Yeah, I know, I'm a slow typist :(
+1 for the suggestion to make it static. He actually can and should.
Heh, you guess still beat me when I was just about to post the answer.
Thanks dasblinkenlight, what is the advantage of using static instead?
@David Generally, it is a good idea to use static functions to indicate their logical independence of the instance. This conveys your intent to human readers of your programs. Less importantly, you can save some CPU cycles by not passing the hidden "this" pointer to your functions.
|
2

The error is that

void AccountDB::output( ostream &s, const Elem *e )

is not declared as const, but you call it from a const method.

Change the declaration (and definition) to:

void output( ostream &s, const Elem *e ) const;

You can do this since you're not modifying any members inside the function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.