Hi, I am trying to work on creating a linked list of player in a game, but I have not written code in a while and have no idea what I am doing wrong, can you please help with suggestion of what I did wrong or idea of how can I improve my code... Thanks

This is the h.file for my main

#pragma once
#include <cstdlib>
#include <sstream>
#include <string>
#include <windows.h>
using namespace std;

template <typename T>
class CNode
{
private:
	CNode<T>* m_pNext;
	T m_value;
public:
	CNode(T value);
	virtual ~CNode();
	T& Value();
	CNode<T>* getNext();
	void setNext(CNode<T>* address);
	string ToString();
	void insert(CNode<T>*& head, CNode<T>*& item); 
};

template <typename T>
CNode<T>::CNode(T value)
{
	m_value = value;
	m_pNext = NULL;
}

template <typename T>
CNode<T>::~CNode()
{
}

template <typename T>
CNode<T>* CNode<T>::getNext()
{
	return m_pNext;
}

template <typename T>
void CNode<T>::setNext(CNode* address)
{
	m_pNext = address;
}

template <typename T>
T& CNode<T>::Value()
{
	return m_value;
}

//template <typename T>
//void CNode<T>::insertplayer(CNode<T>*& player, CNode<T>*& beforeplayer)
//{
//	if (beforeplayer == NULL)
//	{
//	
//	}
//	else
//	{
//		player->setNext(beforeplayer);
//	}
//
//}

template <typename T>
string CNode<T>::ToString()
{
	
	stringstream result;
	CNode<T>* ptr = this;
	while (ptr !=NULL)
	{
		
		result<<"Player "<<ptr->Value()<<endl;
		ptr = ptr->getNext();
		if (ptr != NULL)
		{
			result << "->";			
		}
	}
	return result.str();
}
template <typename T>
void CNode<T>::insert(CNode<T>*&head, CNode<T>*&item)
{
	if (head == NULL)
	{
		head = item;
	}
	else
	{
		head->setNext(item);
		head = item;
		
	}
}

And below is my actual main code

//Platform: C++ Win32 Console Visual Studio 2010
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <stdlib.h>
#include <crtdbg.h>
#include <iomanip>
#include <windows.h>
#include <string>
using namespace std;
#include "CNode.h"

int main()
{
	{
		CNode<string>* head = NULL;
		CNode<string>* ptr;

		string players[8] = {"Becky","Aaron","John","Magret","Susan","Andrea","Morgan Freeman","Carmelo"};
		
		for (int i=0; i<8; i++)
		{
			string playrs = players[i];
			cout<< playrs << endl;
			ptr = new CNode<string>(playrs);
			ptr->insert(head,ptr);
		
			
		}
		
		
		cout << head->ToString()<<endl;
		
	}
	_CrtDumpMemoryLeaks();

	cout << endl;
	system("pause");
	return 0;
}

I cant get them to properly link and I really dont understand why, I think the addresses are not linking.

the insertion code is not accurate you need to change it to :

void CNode<T>::insert(CNode<T>*&head, CNode<T>*&item)
{
	if (head == NULL)
	{
		head = item;
	}
	else
	{
		CNode<T>* temp=head;
		while(temp->m_pNext!=NULL)
		{
			temp=temp->getNext();
		}
		temp->setNext(item);
	}
}
Member Avatar for jmichae3

you get a link error complaining about winmain_16 or something similar because there is no main(). this is just a header file. include a .cpp file which has a main() and uses this header file and you should be set to debug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.