Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Template Travesty


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 lordimmortal2   Members   -  Reputation: 130

Like
0Likes
Like

Posted 10 May 2012 - 08:53 PM

I have some code that tries to create a linked list out of vectors and using templates for the first time I guess it's inevitable that it'll be wrong =P.

The relevance (LinkedList.h):

#pragma once
#include <vector>
#include "Main.h"
template <typename T>
int FindEmptyID(std::vector<T*> &vector)
{
if(list.empty())
	{
		return DEFAULT_ID;
	}
	else
	{
		typename std::list<T*>::iterator i = list.begin();
		int previousId = (*i)->ID();
		i++;
		for(; i != list.end(); i++)
		{
   int currentId = (*i)->ID();
			if(currentId - previousId > 1)
			{
	return previousId + 1;
			}
   previousId = currentId;
		}
  return previousId + 1;
}
}
template <typename T>
T * cLinkedList <T>::ScanListPrevious(std::vector<T*> &vector, int ID)
{
T *previous = 0;
for(typename std::list<T*>::iterator i = list.begin() ; i != list.end() ; ++i)
	{
		T *object = *i;
		if(object->ID() == ID)
		{
			return previous;
		}
		previous = object;
}
  
return 0;
}
template <typename T>
T * cLinkedList <T>::ScanList(std::vector<T*> &vector, int ID)
{
for(typename std::list<T *>::iterator i = list.begin(); i != list.end(); ++i)
	{
		T *object = *i;
		if(object->ID() == ID)
		{
			return object;
		}
	}
	return 0;
}

LinkedList.h gets called in (and used only in) TileContainer.h where it's used to typecast some things.

TileContainer.h:

#pragma once
#include "LinkedList.h"
#include "sSprite.h"

class LayerContainer
{
public:
	 int ID;
	 LayerContainer();
	 ~LayerContainer();
private:
	 LayerContainer* layers;
};

class TileContainer
{
public:
	 sSprite background, middle, foreground; // Graphical rendering of the tile
	 int rowPos, colPos; // Position of the tile
	 int rowSpr, colSpr; // Position of the sprite from the sprite sheet to render
	 int ID;
	 int layerNum;

// Function declarations
	 TileContainer();
	 ~TileContainer();
	 void AddTile(int row, int column, int size, int rowSpr, int colSpr);
	 void ChangeTileSprite(sSprite sprite, int depth, int ID);
	 void AddTileLayer(sSprite sprite, int row, int col);
	 void AddTileLayer(sSprite sprite, int ID);
private:
	 TileContainer* tiles;
};

typedef std::vector<LayerContainer> LayerContainer;
typedef std::vector<TileContainer> TileContainer;


The errors:

Posted Image

I'm not quite sure what's wrong with it and the errors don't really help me (perhaps if I knew more about templates), so if anyone could point me in the right direction it'd me much appreciated.
Prove me wrong so I can know what's right.

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 38849

Like
0Likes
Like

Posted 10 May 2012 - 10:00 PM

Where is the class cLinkedList declared?

#3 lordimmortal2   Members   -  Reputation: 130

Like
0Likes
Like

Posted 10 May 2012 - 10:38 PM

Where is the class cLinkedList declared?


That's a good question =P. They were supposed to be free functions, but for some reason I made them a part of a class and then deleted the class and...this is what I get for going back to year-old code. Thanks for the help, should've realized that sooner. Fixed it =).
Prove me wrong so I can know what's right.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS