Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7790447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:35:26+00:00 2026-06-01T21:35:26+00:00

so im having a bit of problem with my ResourceManager class for a game

  • 0

so im having a bit of problem with my ResourceManager class for a game im working on with c++.
so i tried to make a template function out of my regular addImage function so it will add sounds too but i got some errors which i cant really handle can you guys help me? 😀

.hpp

#ifndef RESOURCE_MANAGER_HPP
#define RESOURCE_MANAGER_HPP

#include "Image.cpp"
#include "SoundBuffer.cpp"
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

typedef std::map<std::string, sz::Image*> ImagesContainer;
typedef std::map<std::string, sz::Image*>::iterator ImagesContainerIt;
typedef std::map<std::string, sz::SoundBuffer*> SoundsContainer;
typedef std::map<std::string, sz::SoundBuffer*>::iterator SoundsContainerIt;
typedef std::map<std::string, sf::Music*> MusicContainer;
typedef std::map<std::string, sf::Music*>::iterator MusicContainerIt;

namespace sz
{
        //      meanwhile this class is only for images, need to edit later for
        //      it to be also able to load sounds, etc...
        class ResourceManager{
                private:
                ResourceManager() {};
                ResourceManager(ResourceManager const&) {};
                static ResourceManager *rm;
                // add functions and variables here
                ImagesContainer imagesContainer;
                SoundsContainer soundsContainer;
                MusicContainer  musicContainer;
                template <class type>
                void AddNew(std::string imagePath);

                public:
                static ResourceManager *Instance();
                // add functions here
                template <class type>
                type *Get(std::string imagePath);
        };
}

#endif

.cpp

#include "ResourceManager.hpp"
#include <typeinfo>

namespace sz
{
        ResourceManager *ResourceManager::rm = NULL;  

        ResourceManager *ResourceManager::Instance()
        {
           if (!rm)
                  rm = new ResourceManager;

           return rm;
        }

        template <class type>
        void ResourceManager::AddNew(std::string filePath)
        {
                type *item = new type(filePath);
                if(typeid(type) == typeid(sz::Image))
                        imagesContainer[filePath] = item;
                else if(typeid(type) == typeid(sz::SoundBuffer))
                        soundsContainer[filePath] = item;
                else
                        return;
        }

        template <class type>
        type *ResourceManager::Get(std::string filePath)
        {
                if(typeid(type) == typeid(sz::Image))
                {
                        ImagesContainerIt it = imagesContainer.find(filePath);
                        if(it == imagesContainer.end())
                        {
                                AddNew<type>(filePath);
                        }
                        it = imagesContainer.find(filePath);
                        return it->second;
                }

                else if(typeid(type) == typeid(sz::SoundBuffer))
                {
                        SoundsContainerIt it = soundsContainer.find(filePath);
                        if(it == soundsContainer.end())
                        {
                                AddNew<type>(filePath);
                        }
                        it = soundsContainer.find(filePath);
                        return it->second;
                }

                else
                        return NULL;
        }
}

the errors @_@

g++ -Wall -c "Sprite.cpp" (in directory: /home/gannash/Desktop/Open Heroes/Engine)
In file included from Sprite.cpp:2:0:
ResourceManager.cpp: In member function ‘type* sz::ResourceManager::Get(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’:
Sprite.cpp:10:65:   instantiated from here
ResourceManager.cpp:50:15: error: cannot convert ‘sz::SoundBuffer*’ to ‘sz::Image*’ in return
ResourceManager.cpp: In member function ‘void sz::ResourceManager::AddNew(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’:
ResourceManager.cpp:36:5:   instantiated from ‘type* sz::ResourceManager::Get(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’
Sprite.cpp:10:65:   instantiated from here
ResourceManager.cpp:23:4: error: cannot convert ‘sz::Image*’ to ‘std::map<std::basic_string<char>, sz::SoundBuffer*>::mapped_type {aka sz::SoundBuffer*}’ in assignment
ResourceManager.cpp: In member function ‘type* sz::ResourceManager::Get(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’:
ResourceManager.cpp:55:2: warning: control reaches end of non-void function [-Wreturn-type]
Compilation failed.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:35:29+00:00Added an answer on June 1, 2026 at 9:35 pm

    Ok, this is my idea after having a look at the code and error messages.

    You cannot return Image and Sound from the same function like that. They are individual types. And you have to specify what type is going to be returned. When you put the if/else in your template it still will check to see if it can return all those types, and that will fail.

    You have to return a BaseClass*(or whatever you want to call it) that both Image and Sound inherits from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of a problem with getting JBoss working across networks. As
I'm having a bit of a problem figuring out how to get the X/Y
writing a recursive string reverse function out of curiosity, but having a bit of
I'm having a bit of a problem with a singleton class I'm exposing via
I'm having a bit of a problem when it comes to printing out this
I am having a bit of a problem with some mysql. I will try
Having a bit of a problem. Trying to assign a template variable within a
I'm having a bit of a problem solving this trivially-looking feature. In my game,
I'm having a bit problem with reading argument value in haskell: I'm having options
Having a bit of a problem, Due to the source-control set-up we use, each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.