0
class MtmMap {   
    public:    
    class Pair {     
         public:   
         Pair(const KeyType& key, const ValueType& value) :     
             first(key),       
             second(value) { }    
         const KeyType first;    
         ValueType second;    
     };     
    class node {    
        friend class MtmMap;    
        Pair data;    
        node* next;     
        public:    
        node();    
        node(const Pair& pair){
            data.Pair(pair.first , pair.second);
            next = NULL;
        }    
    };
    node* temp = new node(pair);
}

errors:

no matching function for call to 'mtm::MtmMap<int, int, AbsCompare>::Pair::Pair()'
invalid use of 'mtm::MtmMap<int, int>::Pair::Pair'
required from 'void mtm::MtmMap::insert(const
mtm::MtmMap<KeyType, ValueType, CompareFunction>::Pair&) [with KeyType = int; ValueType = int;
CompareFunction = AbsCompare]'

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Christine
  • 113
  • 3
  • 9
  • You're clearly not showing us all of your code. MtmMap is presumably a template, yes? But you're not showing us the template. Also, it helps to indicate which line the error occurs on. – Jack Aidley Jan 21 '13 at 10:13

2 Answers2

0

By defining a constructor for Pair which takes arguments you remove the implicit default constructor that takes no arguments. When you have a member of type Pair in node it therefore has to have arguments passed to it in the initialiser list of node, you need to replace your node constructor with this:

    node(const Pair& pair) : data(pair.first, pair.second) {
        next = NULL;
    }

Which will call the constructor on data properly. Learn Cpp has a tutorial on how initialiser lists work which you may want to read.

Jack Aidley
  • 19,439
  • 7
  • 43
  • 70
0

The first thing that jumps out at me is this line:

node* temp = new node(pair);

It looks like you're trying to new a class within a class definition. That could be part of the problem. Also, this area looks a little confused:

node(const Pair& pair){
  data.Pair(pair.first , pair.second);
  next = NULL;
}

It looks like you're trying to call the Pair constructor directly. I'm not sure if that's valid C++ or not. Really, just define a copy-constructor for Pair like so:

Pair( Pair const& other )
  : first( other.first )
  , second( other.second )
{}

Then, that block becomes

node( Pair const& pair )
  : data( pair )
  , next( NULL )
{}

You might also check into std::pair.

Jacob Robbins
  • 1,860
  • 14
  • 16