Surfer on the fall
Surfer on the fall

Reputation: 733

type/value mismatch while declaring custom comparator in a class

I have a problem with the following class. I get error "Tree.cpp:12:56: error: type/value mismatch at argument 2 in template parameter list for ‘template class std::multiset’ // Tree.cpp:12:56: note: expected a type, got ‘(Tree::compare < )’". I don't understand how I should pass the comparator type in the multiset declaration. Could you help me?

#include <set>
#include <deque>
#include <iostream>
using namespace std;

template <typename T>
class Tree
{
    typedef typename std::multiset<Tree<T>*, typename Tree<T>::compare > NodeSet;

private:
    NodeSet children;
    T content;

public:
    struct compare
    {
        bool operator()( const Tree*& t1, const Tree*& t2 ) const
        {
            cout << "Comparing " << t1->GetContent() << " vs " << t2->GetContent() << endl;
            return t1->GetContent() < t2->GetContent();
        }
    };
    Tree& AppendNode( const T& node )
    {
        Tree* t = new Tree( node );
        AttachTree( t );
        return *t;
    }
    void Clear()
    {
        typename NodeSet::iterator it = children.begin();
        while( children.size() != 0 && children.end() != it )
        {

            children.erase( *it );
            delete *it;
            it++;
        }
    }
    Tree( const T& root )
    {
        content = root;
    }
    void AttachTree( Tree* t )
    {
        children.insert( t );
    }
    void Visit( std::deque<T>& exp ) const
    {
        exp.push_back( content );
        typename NodeSet::iterator it = children.begin();
        while( it != children.end() )
        {
            ( *it )->Visit( exp ); it++;
        }
    }
    Tree()
    {}
    Tree( Tree& c )
    {
        c.DeepCopyTo( this );
    }
    T& operator =( const Tree& b )
    {
        b.DeepCopyTo( this );
    }
    ~Tree()
    {
        cout << "in destructor for" << this << endl;
        Clear();
    }
    void DeepCopyTo( Tree* dest ) const
    {
        dest->content = content;
        typename NodeSet::iterator it = children.begin();
        while( it != children.end() )
        {
            Tree* t = new Tree();
            ( *it )->DeepCopyTo( t );
            dest->AttachTree( t );
            it++;
        }
    }
    void Print()
    {
        typename NodeSet::iterator it = children.begin();
        while( it != children.end() )
        {
            cout << *it << ",";
            it++;
        }
    }
};

int main()
{
    Tree<int> tree( 8 );
    tree.AppendNode( 5 );
}

Upvotes: 1

Views: 186

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76381

You might want to change this line to

typedef 
    typename std::multiset<Tree*, typename Tree::compare > 
    NodeSet;          

Note that compare is a dependent name, so you need to use typename.

Also, you should consider moving the struct compare above this line, as this line references it.

Two more things to notice.

You might want to change compare to

struct compare {
    bool operator()(const Tree* t1, const Tree* t2) const {
    cout << "Comparing " <<t1->GetContent() <<" vs "<<t2->GetContent()<<endl;                                                                                        
        return t1->GetContent() < t2->GetContent();
    }   
};  

unfortunately, GetContent doesn't seem defined anywhere in your code.

Upvotes: 1

Related Questions