link: is not a member of node <T>

We are working on linked lists in our Data Structures class, and our instructor gave us this code to try in our compiler and it's not working for some reason! I commented below where the error is.

ERROR MESSAGE: 'link': is not a member of 'node<T>' CODE C2039

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
#ifndef LISTTYPE__H
#define LISTTYPE__H

#include <iostream>

template <class T>
struct node {
	T info;
};

template <class T>
class ListType {
public:
	ListType(); //constructor
	ListType(const ListType<T>&); // copy constructor
	virtual ~ListType(); // Destructor
	const ListType<T>& operator = (const ListType<T>&);
	virtual bool insert(const T&);
	//virtual bool erase(const T&) = 0;
	//virutal bool find(const T&) = 0;
	template <class U>
	friend std::ostream& operator << (std::ostream&, const ListType<U>&);
protected:
	node <T> *head;
	size_t count;
private:
	void copy(const ListType<T>&);
	void destroy();
};


  template <class T> //insert
bool ListType<T>::insert(const T& item) {
	bool result = false;
	if (count == 0) {
		head = new node <T>;
		head->info = item;
		head->link = NULL; // ERROR IS HERE
		++count;
		result = true;
	}
}

#endif 

Last edited on
head is an object of type node.

Here is the node object:
1
2
3
4
template <class T>
struct node {
	T info;
};

As you can see, it contains a single member variable, named info. It does not contain a member variable named link, so when you try to use the member variable named link, you're trying to use something that doesn't exist. In C++, you can't use member variables that don't exist.
Thank you, So:

1
2
3
4
template <class T>
struct node {
              T link;
};


should fix the issue? xD
What do you think will happen with the line
head->info = item;
if you rewrite the node object like that?

Perhaps you've completely missed the whole point of a linked list. Each element in a linked list is meant to contain some piece of data, and a pointer to the next element in the list.
Last edited on
I'm sorry yes I just meant I am adding link to the struct I didn't mean to remove info
Topic archived. No new replies allowed.