Jump to content
  • Advertisement
Sign in to follow this  
lordimmortal2

Template Travesty

This topic is 2405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

1hft3a.png

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.

Share this post


Link to post
Share on other sites
Advertisement

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 =).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!