Problems with template list class

I need some help compiling this project. I can't figure out why im getting these errors. Either I have a typo somewhere I cant find, or I just dont know why I'm getting the errors. here is the output from the build


1>------ Build started: Project: closelab7, Configuration: Debug Win32 ------
1>Build started 3/26/2011 8:10:23 PM.
1>InitializeBuildStatus:
1> Touching "Debug\closelab7.unsuccessfulbuild".
1>ClCompile:
1> driver.cpp
1>g:\closelab7 solution\closelab7\closelab7.h(41): error C2039: 'front' : is not a member of 'LinkList<T>'
1> with
1> [
1> T=std::string
1> ]
1> g:\closelab7 solution\closelab7\closelab7.h(40) : while compiling class template member function 'std::ostream &operator <<(std::ostream &,const LinkList<T> &)'
1> with
1> [
1> T=std::string
1> ]
1> g:\closelab7 solution\closelab7\driver.cpp(12) : see reference to class template instantiation 'LinkList<T>' being compiled
1> with
1> [
1> T=std::string
1> ]
1>g:\closelab7 solution\closelab7\closelab7.h(42): error C2597: illegal reference to non-static member 'LinkList<T>::First'
1> with
1> [
1> T=std::string
1> ]
1>g:\closelab7 solution\closelab7\closelab7.h(42): error C3867: 'LinkList<T>::First': function call missing argument list; use '&LinkList<T>::First' to create a pointer to member
1> with
1> [
1> T=std::string
1> ]
1>g:\closelab7 solution\closelab7\closelab7.h(44): error C2227: left of '->data' must point to class/struct/union/generic type
1>g:\closelab7 solution\closelab7\closelab7.h(45): error C2227: left of '->next' must point to class/struct/union/generic type
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.59
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


here is the declaration of the class where I am apparently having the problem with the inline operator function
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
//Author: Chris Mouser
//Purpose: Header file for close lab 7: LinkList class
//Date: 3/14/2011
#ifndef LINKLIST
#define LINKLIST
#include <iostream>
#include <new>
#include <cassert>
using namespace std;

template <typename T>
class LinkList
{
private:
	class Node
	{
     public:
		  T info;
		  Node *next;
	};
	typedef Node *nodePtr;

public:
	LinkList();                              //default constructor, written inline
	LinkList (const LinkList <T>& orig);     //copy constructor
	~LinkList();                             //destructor
	bool empty();                            //determine if LinkList is empty
	T Front();                               //return item at front of LinkList
	T Back();                                //return item at back of LinkList
	void Push_Back (const T& item);          //add item at the end of the LinkList
	void Push_Front (const T& item);         //add item at the beginning of the LinkList
	void Pop_Back();                         //remove the last item in the LinkList
	void Pop_Front();                        //remove the first item in the LinkList
	void insert (T item);                    //insert the item in ascending order
	void erase (T item);                     //remove the item in the list
	void search (T item);                    //search for a given item. If it can be found, return true. Otherwise, return false
	int Count();                             //return the number of items in the list
	LinkList<T>& operator=(const LinkList<T> & orig);                 //overloaded assignment operator
	friend ostream &operator << (ostream & out, const LinkList<T>& orig)
	{                                                                   //overloaded output operator, written inline
		nodePtr *nPtr = orig.front;
		while (First != NULL)
		{
			out << First->data << endl;
			First = First->next;
		}

		return out;
	}

private:
	nodePtr First;                          //node pointer that points to front (first node)
};
//end of template class declaration


and the 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
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
//Test driver for Link List
#include "closelab7.h"
#include <iostream>
#include <string>
using namespace std;

void test (LinkList<int> M);

int main (void)
{
	string L1;
	LinkList<string> A;
	LinkList<int> B;
	int i;

	cout << "Created empty linklist of string." << endl;
	if (A.empty() == true)
		cout << "The list A is empty." << endl;
	else
		cout << "The list A is not empty." << endl;

    cout << "Testing copy constructor." << endl;
	test(B);

	cout << "Testing empty list." << endl;
	LinkList<string> A2;
	A2 = A;

	//testing output operator
	cout << "call operator for empty list." << A << endl;

	//Testing insert() function with string list
	cout << "Enter a string to add to list, or \"end\" to stop << ";
	cin >> L1;
	while (L1 != "end")
	{
		A.insert(L1);
		cout << "Enter string to add to list, or end to stop" << endl;
		cin >> L1;
	}
	cout << A << endl;

	//testing insert() with integer list
	cout << "Integer list L is created and displayed:\n";
	LinkList<int> L;
	for (i = 0; i < 5; i++)
		L.insert(i);
	cout << L << endl;

	//testing copy constructor in initialization
	cout << "Copy Constructor. " << endl;
	LinkList<int> C(L), M;
	cout <<" List C is initialized with list L:\n" << C << endl;

	//test of the overloaded assignment operator
	cout << "\n assignment: L\n";
	M = L;

	//testing copy constructor in function call
	cout << "\n calling 'test' \n ";
	test(M);

	for (i = 10; i < 25; i++)
		B.insert(i);
	cout << "Before assignment, LinkList B " << B << endl;

	B.erase(10);
	B.erase(20);
	B.erase(24);
	cout << "Front, middle, and end items deleted from LinkList B. " << B << endl;

	cout << "\n assignment operator B = L\n" << endl;
	B = L;
	cout << "\n B as a copy of L: " << B << endl;
	cout << "\n L remains unchanged after copy: " << L << endl;

	return 0;
}

void test(LinkList<int> M)
{
	cout << "In test \n ";
	cout << "LinkList M " << M << endl;
}


if anyone could help me out it would be appreciated. thank you
I did just a quick glance at the post. I see that the first error reported is that "front" is not a member of LinkList<T>. This is true. It is "Front" and not "front".
The rest of the errors:
1>g:\closelab7 solution\closelab7\closelab7.h(42): error C2597: illegal reference to non-static member 'LinkList<T>::First'
1> with
1> [
1> T=std::string
1> ]
1>g:\closelab7 solution\closelab7\closelab7.h(42): error C3867: 'LinkList<T>::First': function call missing argument list; use '&LinkList<T>::First' to create a pointer to member
1> with
1> [
1> T=std::string
1> ]
1>g:\closelab7 solution\closelab7\closelab7.h(44): error C2227: left of '->data' must point to class/struct/union/generic type
1>g:\closelab7 solution\closelab7\closelab7.h(45): error C2227: left of '->next' must point to class/struct/union/generic type
1>


has to do with this << overload function here:

1
2
3
4
5
6
7
8
9
10
    friend ostream &operator << (ostream & out, const LinkList<T>& orig)
    {                                                                   //overloaded output operator, written inline
        nodePtr *nPtr = orig.front;
        while (First != NULL)
        {
            out << First->data << endl;
            First = First->next;
        }

        return out;


This overlaod function is not a member of LinkList<T> - therefore to use the variable First directly is illegal.
You need to provide an object of LinkList<T> in order to use First.
The whole logic of the << overload function needs attention.
Last edited on
Topic archived. No new replies allowed.