Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I got the next template:
C++
template <typename T>
struct List {
	struct node {
		T key;
		node* prev;
		node* next;
	};
	node* first = nullptr;
	node* last = nullptr;
	// functions
};

Using an outside function I need to compare 2 Lists, but the node pointer List<t>::node* its not declared right, even though the compiller tells me thats the right path. Why doesnt it work?

What I have tried:

C++
template <typename T> bool compare(List<T> L1, List<T> L2)
{
	if (L1.size() == L2.size())
	{
		List<T>::node* currentL1 = L1.first; //error
		List<T>::node* currentL2 = L2.first; //error
		while (currentL1)
			if (currentL1->key != currentL2->key)
				return 0;
			else
			{
				currentL1 = currentL1->next;
				currentL2 = currentL2->next;
			}
	}
	else
		return 0;
	return 1;
}
Posted
Updated 11-Mar-22 4:43am
Comments
Richard MacCutchan 11-Mar-22 10:32am    
What is the exact error message, and where is the actual implementation of the two templates?

typename does the trick, try
C++
//..
typename List<T>::node* currentL1 = L1.first;
typename List<T>::node* currentL2 = L2.first;
// ..
 
Share this answer
 
CPalini's solution is correct, but maybe in modern C++, rather than trying to puzzle out what a variable's type should be, one can use auto instead e.g.
C++
auto * currentL1 = L1.first;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900