Runtime & Compiler Errors with Resource Manager Template Class?

Hello, CPlusPlus community. I am writing a Game Engine and I can't seem to get the hang template classes. I understand the basic principals but when it comes to using smart pointers I'm lost and having lot's of compiler/memory errors.

Please excuse the massive amount of code that is most likely full of hacks because of me trying to fix it.

If anyone could suggest/fix things to fix and provide a detailed answer please do! And I am using SFML!

ResourceManager.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef RESOURCEMANAGER_HPP
#define RESOURCEMANAGER_HPP

#include <iostream>
#include <map>
#include <string>
#include <fstream>

#include <SFML/Graphics.hpp>

/* ResourceManager Template
   Provides resource mapping and template for loading different resource types. */

class ImageManager;

template <class T>
class ResourceManager {
public:
	ResourceManager() {
	}

	virtual ~ResourceManager() {
	}

	void AddResource(const std::string& sID, T* tResource) {
		myResources.insert(std::pair<std::string, *T>(sID, tResource));
	}

	void RemoveResource(const std::string& sID) {
		std::map<std::string, *T>::iterator it;
		it = myResources.find(sID);

		if(it != myResources.end())
			myResources.erase(it);
	}

	std::auto_ptr<T> GetResource(const std::string& sID) {
		std::map<std::string, *T>::iterator it;
		it != myResources.find(sID);

		if(it != myResources.end())
			return it->second;
	}

	virtual bool LoadResource(const std::string& sID, const std::string& sFilename) = 0;

private:
	std::map<std::string, std::auto_ptr<T>> myResources;
};

#endif 


ImageManager.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef IMAGEMANAGER_HPP
#define IMAGEMANAGER_HPP

#include <SFML/Graphics.hpp>

#include "ResourceManager.hpp"

// TO-DO: add singleton

class ImageManager : public ResourceManager<sf::Image> {
public:
	ImageManager::ImageManager();
	ImageManager::~ImageManager();

	virtual bool LoadResource(const std::string& sID, const std::string& sFilename);

	sf::Sprite* GetSprite();

private:
	sf::Sprite* mySprite;
};

#endif 


ImageManager.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "ImageManager.hpp"

ImageManager::ImageManager() {
}

ImageManager::~ImageManager() {
}

bool ImageManager::LoadResource(const std::string& sID, const std::string& sFilename) {
	sf::Image* tempImage;
	if(!tempImage->LoadFromFile(sFilename))
		return false;
	
	mySprite->SetImage(*tempImage);

 	AddResource(sID, tempImage);

	return true;
}

sf::Sprite* ImageManager::GetSprite() {
	return mySprite;
}
Last edited on
¿which errors?

1
2
std::map<std::string, std::auto_ptr<T> > myResources;
myResources.insert(std::pair<std::string, *T>(sID, tResource));
T*, not *T. However a pointer it is not an auto_ptr, so it should be myResources.insert( std::make_pair(sID, auto_ptr<T>(tResource)) );

1
2
3
4
5
6
7
	std::auto_ptr<T> GetResource(const std::string& sID) {
		std::map<std::string, *T>::iterator it;
		it != myResources.find(sID);

		if(it != myResources.end())
			return it->second;
	}
warning: control reaches end of non-void function.
Also, when you copy an auto_ptr the source lost the ownership and becomes non-dereferenceable

¿Why the forward declaration of ImageManager inside ResourceManager.hpp ?
Thank you! And the forward declaration was just me trying different things to fix it, I realize now that I must of been tired when I did that :P
Topic archived. No new replies allowed.