Classes and templates

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
template <class __Key, class __Value>
class Node
{
    public:
        typedef Node <__Key, __Value>* Ptr;
        Node(__Key & __k, __Value & __v)
        {
            key = __k;
            value = __v;
        }

        __Key & key();
        __Value & value();

        bool hasNext() const
        {
            return next != NULL;
        }

        Ptr  getNext() const
        {
            return next;
        }

        Ptr insert(__Key & __k, __Value & __v);

    private:
        Ptr next;
};


This is the startup code for an assignment, now just to make sure everything is ok.
(1) I tried creating an instance of Node by writing
 
Node <string, int>  testnode;


I got the following error: no matching function for call... (I solved this by declaring a pointer for Node, but then member functions didn't work)

Why is that?

(2) What does this mean? And why do we use the ampersands? Are these constructors or what?
1
2
3
__Key & key();
__Value & value();


Thanks for any help
Last edited on
Your first error for Node <string, int> testnode; is because you don't have a default constructor defined in your class. So either define one, or instantiate your class with the proper values.

the __Key & key() lines look like functions that return a reference to a __Key object. You probably need to say __Key key; to define your class variable.
1. Node <string, int> testnode;
That means create a Node using the default constructor (constructor which takes no parameters) and you do not have such a constructor.
If you have a constructor which takes parameters - like you have _ then if you want/need a default
constructor, then you the programmer must provide it - the compiler will not provide it.

2. You have some serious issues here.
Written like this, these are function declarations:
1
2
__Key & key();
__Value & value();


Which means that these statements in the constructor are incorrect:
1
2
3
4
5
Node(__Key & __k, __Value & __v)
{
    key = __k; //error - key is not a variable
    value = __v; //error - value is not a variable
}

Which leads us to another issue:
Where are you goung to store the values passed
in the constructor ( __k and __v) ?
Exactly!
I know I should add key and value under private so I can use the constructor properly.
These "errors" came with the code, I don't know if they were put there intentionally.
Thanks for the help.
Topic archived. No new replies allowed.