typedef const reference

Hello all
Im working on an assignment to implement the functionality of the list class. I've got it 90% done, but the typedefs for references are giving me troubles.
Part of the assignment was to put the class header code, class .cpp code, and the main .cpp code all into one file, so that's why they're not split up, and they need to stay that way.

When I put the typedefs inside the public class portion, the function implementation says it does not name a type (Sorry if Im not using the right terminology, what I mean is where you actually write the code for the function, not the prototype).

When I put the typedefs in the #include area, I get errors of "candidates are const element_type& linkedlist::_____() const" and later in the implementation part, errors of "prototype does not match any in class linked list".



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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <cstdlib>
#include <string>
#include <iostream>

typedef int element_type;
typedef element_type& reference;
typedef const element_type& const_reference;
using namespace std;

class linkedlist {
public:

//    typedef element_type& reference;
//    typedef const element_type& const_reference;
    linkedlist(); //default constructor for empty list
    ~linkedlist(); //destructor to free nodes dynamically created to support the linklist
    bool empty() const;
    void clear();
    reference back();
    const_reference back() const;
    reference front();
    const_reference front() const;
    linkedlist& operator=(const linkedlist& l);
    void pop_back( );
    void pop_front( );
    void push_back( const element_type& x );
    void push_front( const element_type& x );
    void sort( );
    
    // constructor that initializes the linked list with n nodes,
    // with elem value from 0 to n-1
    explicit linkedlist(unsigned int n);
    
    // print the linked list in the forward direction,
    // similar to the show function of lab6
    void check() const;
    
    // print the linked list in the backward direction,
    // similar to the reverse_show function of lab7
    void rcheck() const;

    int nodereturn(int x) const;
    int getsize() const;
    
private:

    
    struct Node
    {
        element_type elem;
        Node * next;
        Node * prev;
    };
    
    Node * head;
    Node * tail;
    Node * current;
    Node * previous;
    int size;

};


linkedlist::linkedlist()
{
    head = new Node;
    tail = new Node;
    head->next = tail;
    head->prev = NULL;
    tail->next = NULL;
    tail->prev = head;
    size = 0;
    
}


And here's the function implementation portion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
reference linkedlist::front()
{
    
}

const_reference linkedlist::front()
{
    
}

reference linkedlist::back()
{
    
}

const_reference linkedlist::back()
{
    
}


Where should I be putting the reference typdefs so they work?
Last edited on
/facepalm

Solved it myself.
I failed to put const at the end of the function implementation for the const_reference ones.
It all compiles now.
A better question then, what exactly should I be returning in those functions?

I tried

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
reference linkedlist::front()
{
    return &(head->elem);
}

const_reference linkedlist::front() const
{
    
}

reference linkedlist::back()
{
    return &(tail->elem);
}

const_reference linkedlist::back() const
{
    
}


and it didn't like that.
Topic archived. No new replies allowed.