template list container

I have three functions in a header file and a driver. I cant figure out why my driver isnt recognizing the member functions in my header. Any help?

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
//Author: Chris ******
//Date: 4/20/2011
//Purpose: header file for closed lab 11: list container
#include <iostream>
#include <vector>
using namespace std;

template <typename T>
class List
{
public:
	void InputList(ostream & out, const list<T> & L);
	void fillIn(const list<T> & L, vector<T>& V);
	void changeValue(const list<T> & L, T item1, T item2);

private:
	list<T>& L const;
	vector<T>& V const;

}; //end of class declaration

//Definition of void inputList
template <typename T>
void inputToList(ostream & out, const list<T> & L)
{
	ifstream in;
	while (in.open() || !in.eof())
	{
		for (int i = 0; i != in.eof(); i++)
		{
			L[i];
			L.push_back(i);
		}
	}

	return out;
}

//Definition of FillIn()
template <typename T>
void FillIn(const list<T>& L, vector<T> & V)
{
	for (int i = 0; i < L.end(); i++)
	{
		L[V[i]];
		L.push_back(v[i]);
	}
}

//Definition of changeValue
template <typename T>
void changevalue(const list<T>& L, T item1, T item2)
{
	double item1, item2;
	cout << "Enter values to swap: ";
	cin >> item1 >> item2;
	double found1 = find(L.begin(), L.end(), item1);
	double found2 = find(L.begin(), L.end(), item2);
	while (!empty())
	{
		L.insert(found1, 1, found2);
		L.erase(found);
	}

	cout << "Swapped " << found1 << "with " << found2 << endl;
}



Driver

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
//Author: Chris ******
//Date: 4/20/2011
//Purpose: driver for closed lab 11 list container
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
#include "List.h"
using namespace std;

template <typename T>
ostream & operator << (ostream & out, const list<T> & aList)
{
	for (list<T>::const_iterator it = aList.begin(); it != aList.end(); it++)
		out << *it << "  ";
	return out;
}

int main()
{
	list<double> dList;
	
	double item1, item2;

	for (int i = 0; i < dList.end(); i++)
	{
		dList[i];
		dList.push_back(i);
	}

 //testing input function
	ifstream in;
	string FileName;
	cin >> FileName;
	dList.InputList(FileName);

//testing fill in function
	int fill;
	cout << "Enter value to fill in: ";
	cin >> fill;
	dList.FillIn(fill);


	cout << "Change values? ";
	char yorn;
	cin >> yorn;
	while (yorn == "y")
	{
		changeValue(dList, item1, item2);
	}
	return 0;
}



Last edited on
If you take the function definition outside the class, you must prepend the function's name with the class name:

1
2
3
4
5
6
7
8
9
10
class SampleClass
{
    void MyFunction(void);
}

//Definition:
void SampleClass::MyFunction(void)
{
    cout << "My Function!" << endl;
}
Dont I have to put the parameters there? Are you talking about the header or the driver?
Better? It still isnt recognizing the member functions...

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
//Author: Chris ******
//Date: 4/20/2011
//Purpose: header file for closed lab 11: list container
#include <iostream>
#include <vector>

#ifndef LIST
#define LIST
using namespace std;

template <typename T>
class List
{
public:
	void InputList(ostream & out,  list<T> & L);
	void fillIn(const list<T> & L, vector<T>& V);
	void changeValue(const list<T> & L, T item1, T item2);

private:
	const List<T>& L;
	const vector<T>& V;

};

 //end of class declaration

//Definition of void inputList
template <typename T>
void List<T>::InputList(ostream & out, const list<T> & L)
{
	ifstream in;
	while (in.open() || !in.eof())
	{
		for (int i = 0; i != in.eof(); i++)
		{
			L[i];
			L.push_back(i);
		}
	}

	return out;
}

//Definition of FillIn()
template <typename T>
void List::fillIn(const list<T>& L, vector<T> & V)
{
	for (int i = 0; i < L.end(); i++)
	{
		L[V[i]];
		L.push_back(v[i]);
	}
}

//Definition of changeValue
template <typename T>
void List::changeValue(const list<T>& L, T item1, T item2)
{
	double item1, item2;
	cout << "Enter values to swap: ";
	cin >> item1 >> item2;
	double found1 = find(L.begin(), L.end(), item1);
	double found2 = find(L.begin(), L.end(), item2);
	while (!empty())
	{
		L.insert(found1, 1, found2);
		L.erase(found);
	}

	cout << "Swapped " << found1 << "with " << found2 << endl;
}

#endif 


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
//Author: Chris ******
//Date: 4/20/2011
//Purpose: driver for closed lab 11 list container
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
#include "List.h"
using namespace std;

template <typename T>
ostream & operator << (ostream & out, const list<T> & aList)
{
	for (list<T>::const_iterator it = aList.begin(); it != aList.end(); it++)
		out << *it << "  ";
	return out;
}

int main()
{
	list<double> dList;
	list<int> L;
	double item1, item2;

	for (int i = 0; i < dList.end(); i++)
	{
		dList[i];
		dList.push_back(i);
	}

 //testing input function
	ifstream in;
	string FileName;
	cin >> FileName;
	dList.InputList(FileName);

//testing fill in function
	int fill;
	cout << "Enter value to fill in: ";
	cin >> fill;
	dList.FillIn(fill);


	cout << "Change values? ";
	const char yorn;
	cin >> yorn;
	while (yorn == "y")
	{
		changeValue(dList, item1, item2);
	}
	return 0;
}

Yes, better, but you still missed the <T> part in a couple of functions. InputList() looks OK, but the others are not.

Refer to http://www.cplusplus.com/doc/tutorial/templates/.
Topic archived. No new replies allowed.