C++ iterator, function not running

133:40: error: \u2018remove\u2019 declared as function returning a function
(typename vector <T>::iterator ix, T n)

this is the error i get and i can't seem to get it to run. any help would be appreciated.
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <typename T>
   class Mlist
   {
   public:
    Mlist();//creates the list 
    T front();//returns the front of the list 
    T last();//returns the end of the list
    bool in(T x);//returns true if x is in the list and false otherwise
    bool empty(); // returns true if the list is empty
    void addfront(T entry);//add entry to the back of the list
    void addend(T entry);//add entry to the back of the list
    void addorder(T entry);//add entry to an ordered list
    void removefront();//removes the front of the list
    void removeend();//removes the back of the list
    void remove(T n);//searches the list and removes the entry with value n
    private:
    vector<T> mlist;
    void remove(typename vector<T>::iterator ix, T n);//uses an iterator and recursion to remove value n
    void addorder(typename vector<T>::iterator ix, T n);//uses an iterator and recursion to add value n in an ordered list
    }; // mlist

template <typename T>
    Mlist<T>::Mlist()
    {

    }

template <typename T>
    T Mlist<T>::front()
	{
	    T x;
	    if(empty())
		{
		    return x;
		}
	    else
	    	{
		    return *mlist.begin();
		}
	}

template <typename T>
    T Mlist<T>::last()
	{
	    T x;
	    if(empty())
		{
		    return x;
		}
	    else
	    	{
		    return *(--mlist.end());
		}
	}

template <typename T>
   bool Mlist<T>::in (T x)
    {
	for(int i = 0; i < mlist.size();)
	    {
		if(mlist[i] ==x)
	    	    {
			return true;
		    }
	    }
    
	return false;
    }

template <typename T>
    bool Mlist<T>::empty()
    {
	if(mlist.size() == 0)
	    {
		return true;
	    }
	else
	    {
		return false;
	    }
    }

template <typename T>
    void Mlist<T>:: addfront(T x)
	{
	    if(this ->empty())
		{
		    mlist.push_back(x);
		}
	    else
		{
		    mlist.insert(mlist.begin(),x);
		}
	}

template <typename T>
    void Mlist<T>:: removefront()
	{
	    if(!this ->empty())
		{
		    mlist.erase(mlist.begin());
		}
	}

template <typename T>
    void Mlist<T>:: removeend()
	{
	    if(!this ->empty())
		{
		    (--mlist.end());
		}
	}

template <typename T>
    void Mlist<T>::addorder (T entry)
	{
	    if(this ->empty())
		{
		    addorder(mlist.begin(), entry);
		}
	    else
		{
		    mlist.push_back(entry);
		}
	}

template <typename T>
    void Mlist<T>::remove(T n)
	(typename vector <T>::iterator ix, T n)
	{
	    if(ix==mlist.end())
		{
		    return
		}
		if (*ix ==n)
		{
		    mlist.erase(ix, n);
		    remove(ix);
		}
		else
		{
		    remove(++ix, n);
		}
	}

int main()
{
 Mlist<int> test1=Mlist<int>() ;
 test1.addend(5);
 test1.addend(7);
 test1.addend(4);
 test1.remove(7);
 cout << test1.front()<< endl;
 cout << test1.last()<< endl;
 Mlist<string> test2= Mlist<string>() ;
 test2.addend("John");
 test2.addend("Paul");
 test2.addend("Mary");
 test2.addend("Kate");
 test2.remove("Paul");
 cout << test2.front()<< endl;
 cout << test2.last()<< endl;
}
What is on the line 133?

You have essentially:
1
2
3
4
5
void      // return type
foo       // name
( int n ) // arguments
( double y, int n ) // mysterious line 133
{}        // function body 
i guess i was suppose to move the line next to remove;

so now it looks like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <typename T>
    void Mlist<T>::remove (typename vector <T>::iterator ix, T n)
	{
	    if(ix==mlist.end())
		{
		    return;
		}
		if (*ix ==n)
		{
		    mlist.erase(ix, n);
		    remove(ix);
		}
		else
		{
		    remove(++ix, n);
		}
	}


but now i get the error:
/tmp/cc54uecp.o: In function `main':
hw4.cpp:(.text+0x67): undefined reference to `Mlist<int>::remove(int)'
hw4.cpp:(.text+0x239): undefined reference to `Mlist<std::string>::remove(std::string)'
collect2: error: ld returned 1 exit status
Of course.

Within your class definition (lines 5-24) you do declare (on line 19) that Mlist has member void remove( T n )

However, there is no implementation for that member function anywhere, so linker cannot link them in and gives the error message.


On lines 131-~148 you do have a template implementation for (non-existent) member
void remove(typename vector <T>::iterator ix, T n)


If you had non-templates:
1
2
3
4
5
6
7
class Foo {
  void bar( int );
};

void Foo::bar( double ) { // compiler error
  // class Foo does not have member bar(double)
}
Topic archived. No new replies allowed.