Sign in to follow this  
Surg AKA Kunark

Template Structures

Recommended Posts

Hey, Im programming a linkedlist that works with a Structure and class, for some reason I get this error.
error C2955: 'SNode' : use of class template requires template argument list

What exactly is a Template Arguement List? Here is my code.
#pragma once
#include <iostream>
using namespace std;

template <typename T>
struct SNode
{
	T m_iData;
	SNode* m_pNext;
};

template <typename T>
class CLinkList
{
public:
	CLinkList<T>() : m_pHead(0) {};
	void AddNode(T iData);
	void Display();
private:
	SNode* m_pHead;
};

template <typename T>
void CLinkList<T>::AddNode(T iData)
{
	SNode* pNew = new SNode;
	pNew->m_iData = iData;
	pNew->m_pNext = m_pHead;
	m_pHead = pNew;
}

template <typename T>
void CLinkList<T>::Display()
{
	SNode* pCurrent = m_pHead;
	while(pCurrent)
	{
		cout << pCurrent->m_iData << endl;
		pCurrent = pCurrent->m_pNext;
	}
}

And main
#include "LinkList.h"

int main()
{
	CLinkList <int> I;
	I.AddNode(5);
	I.AddNode(2);
	I.AddNode(3);
	I.Display();

	//try more data types here!!
	return 0;
}

Share this post


Link to post
Share on other sites
Where ever you use SNode*, replace it with SNode<T>*, except in SNode itself. The compiler doesn't know that you want SNode to use the same type as your other classes because they are completely seperate from its point of view so you have to tell it.

Share this post


Link to post
Share on other sites
Whenever you use SNode in your template class you need to declare what type of Snode you want, probably an SNode<T>. You can't just use SNode and expect the compiler to figure out what kind of SNode you need.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this