Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Linked List Error

We are assigned to write a program that reads in file data, puts it in an object and links objects together in a linked list. The compiler gives me an error that says "cannot deduce template argument for 'const NodeT<Type> &' from 'College'".

This error occurs when this function is executed:

/*
* pre: Linked list structure exists to add nodes to
* post: Linked list exists and contains College data
* effect: Reads in data from schools.dat and sets up a linked list by
adding new nodes to end of list
*/
void get_list(NodeT<College>* &head_ptr)
{
ifstream data_in;
string szCollege_Name;
int iTuition;
int iEnrollment;

// Open file
data_in.open("schools.dat");
assert(!data_in.fail());

while(getline(data_in, szCollege_Name))
{
data_in >> iTuition >> iEnrollment;
College colleges(szCollege_Name, iTuition, iEnrollment);
list_tail_attach(head_ptr, colleges);
data_in.ignore(80, '\n');
}

// Close file
data_in.close();
}

And that function calls the list_tail_attach function:

/*
Precondition: head_ptr is the head pointer of a linked list.
Postcondition: A new NodeT containing the given entry has been added at
the tail of the linked list; if this happens to be the
first NodeT of the linked list, then head_ptr now points to
this NodeT (otherwise head_ptr is unchanged).
*/
template <class Type>
void list_tail_attach(NodeT<Type>*& head_ptr, const NodeT<Type>& entry)
{
NodeT<Type> *index_ptr = NULL;

if (head_ptr == NULL)
{
list_head_insert(head_ptr, entry);

} else {

index_ptr = head_ptr;

while (index_ptr->link() != NULL)
{
index_ptr = index_ptr->link();
}

list_insert(index_ptr, entry);
}
}

Everything works fine unless I call the list_tail_attach method, in which case the above compiler error message happens. My professor has no idea what that error means or how to fix it, so I was hoping someone here could shed a little more light on this matter.
[2360 byte] By [Dark Rain] at [2007-11-11 10:23:35]
# 1 Re: Linked List Error
is your code actually instantiating the College instances? Can you print the names and other data from the instance?
nspils at 2007-11-11 20:59:02 >
# 2 Re: Linked List Error
Up above I have this line:

NodeT<College> *head_ptr = new NodeT<College>;
head_ptr = NULL;

I haven't actually tried just printing something without using the node class, but I would assume it would work since the error is being drawn on the list_tail_attach call.
Dark Rain at 2007-11-11 21:00:03 >
# 3 Re: Linked List Error
I am trying to evaluate where the problem is, based upon the error message. The compiler wouldn't know how to resolve a call to type a "College" object if it doesn't know what defines a College object.

I don't know if the issue is the node class or the linked list.

You are constructing what appears to be a College node in your statement:

College colleges(szCollege_Name, iTuition, iEnrollment);

can you pass data into the colleges node? can you get data (name, tuititon, enrollment) out of it? can you add an instance to an array or vector?
nspils at 2007-11-11 21:00:56 >
# 4 Re: Linked List Error
It seems I can use the get and set methods associated with the College class without error. The problem comes with the linked list, for some reason Visual Studio 2005 doesn't like the second argument to the list_tail_attach function, the one that attempts to define entry as the college object.

If it helps the full error is this:

Error C2784: 'void list_tail_attach(NodeT<Type> *&,const NodeT<Type> &)' : could not deduce template argument for 'const NodeT<Type> &' from 'College'
Dark Rain at 2007-11-11 21:01:57 >
# 5 Re: Linked List Error
have you tried removing the "const-ness" of the node reference in the argument?
nspils at 2007-11-11 21:03:01 >
# 6 Re: Linked List Error
I think, instead of this:
...
College colleges(szCollege_Name, iTuition, iEnrollment);
list_tail_attach(head_ptr, colleges);
...
you should try this:
...
NodeT<College> * newNode = new NodeT<College>;
... // initialize College part of newNode,
list_tail_attach(head_ptr, *newNode);
...
You have to initialize the College part of newNode. Supposing NodeT class is defined like this:
template< class T >
class NodeT
{
public:
NodeT(const T & v)
: mValue(v)
{
}
T mValue;
};
the initialization can look like this:
NodeT<College> * newNode = new NodeT<College>(College(szCollege_Name, iTuition, iEnrollment));
I hope this helps.
Viorel at 2007-11-11 21:04:07 >
# 7 Re: Linked List Error
The constructor of NodeT.h looks as follows:

template <class Type>
class NodeT
{
public:
// TYPEDEF
typedef Type value_type;

// CONSTRUCTOR
NodeT(const value_type& init_data = value_type(),NodeT* init_link = NULL)
{
data_field = init_data;
link_field = init_link;
}

I tried what you said and have this:

ifstream data_in;
string szCollege_Name;
int iTuition;
int iEnrollment;

// Open file
data_in.open("schools.dat");
assert(!data_in.fail());

while(getline(data_in, szCollege_Name))
{
data_in >> iTuition >> iEnrollment;
NodeT<College> * newNode = new NodeT<College>(College(szCollege_Name, iTuition, iEnrollment));
list_tail_attach(head_ptr, *newNode);
data_in.ignore(80, '\n');
}

// Close file
data_in.close();

The old error is gone, but I am getting a new one that says:

error C2664: 'NodeT<Type>::NodeT(const College &,NodeT<Type> *)' : cannot convert parameter 1 from 'const NodeT<Type>' to 'const College &'

These templates are turning out to be a pain :\. Thanks for the help.
Dark Rain at 2007-11-11 21:05:05 >
# 8 Re: Linked List Error
The notation Node<College> indicates that the node contains a College object. You should have a class definition for College, and you should have a class definition for Node<College> which, as its "entry" has a College object. Your constructor for the Node class can take the data needed to initialize the College object held in the node, and put the (parent and) child pointer(s) as null. Then, when you call the insert_at_tail method, the child pointer is set.
nspils at 2007-11-11 21:06:03 >
# 9 Re: Linked List Error
I'm still a bit confused. The College class has a constructor that looks like this:

College(const string& init_name = "", int init_tuition = 0, int init_enrollment = 0)
: name(init_name), tuition(init_tuition), enrollment(init_enrollment)
{

} //initializer list

As the error says it can't convert from const NodeT<Type> to const College &. Now you mentioned something about having another College class declaration that tells the compiler what to do with NodeT<College>, but I'm not sure how to go about doing this.

Would I do the exact same thing but put a constructor below that says something like:

NodeT<College>(const string& init_name = "", int init_tuition = 0, int init_enrollment = 0)
: name(init_name), tuition(init_tuition), enrollment(init_enrollment)
{

} //initializer list
Dark Rain at 2007-11-11 21:07:06 >
# 10 Re: Linked List Error
NodeT<College>(const string& init_name, int init_tuition, int init_enrollment)
{
College college( init_name, init_tuition, init_enrollment );
NodeT<College> *next = null;
}
nspils at 2007-11-11 21:08:08 >
# 11 Re: Linked List Error
Alright, so I have worked everything out (or so I thought), and am now getting this odd error:

error C2061: syntax error : identifier 'value_type'

on this line:

template <class Type> void list_head_insert(NodeT<Type>*& head_ptr,
constNodeT<Type>::value_type& entry);

I have a typedef defined, so nothing should be thrown here. I asked my instructor and she tested it on VS 6.0 and it compiled fine, however it obviously doesn't on VS 2005. Her solution was get another compiler, but I know VS 2005 can't be this broken :(
Dark Rain at 2007-11-11 21:09:11 >