English 中文(简体)
缩略语
原标题:Template function messed up

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? :D

.hpp

#ifndef RESOURCE_MANAGER_HPP
#define RESOURCE_MANAGER_HPP

#include "Image页: 1"
#include "SoundBuffer页: 1"
#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

页: 1

#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页: 1" (in directory: /home/gannash/Desktop/Open Heroes/Engine)
In file included from Sprite页: 1:2:0:
ResourceManager页: 1: In member function ‘type* sz::ResourceManager::Get(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’:
Sprite页: 1:10:65:   instantiated from here
ResourceManager页: 1:50:15: error: cannot convert ‘sz::SoundBuffer*’ to ‘sz::Image*’ in return
ResourceManager页: 1: In member function ‘void sz::ResourceManager::AddNew(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’:
ResourceManager页: 1:36:5:   instantiated from ‘type* sz::ResourceManager::Get(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’
Sprite页: 1:10:65:   instantiated from here
ResourceManager页: 1:23:4: error: cannot convert ‘sz::Image*’ to ‘std::map<std::basic_string<char>, sz::SoundBuffer*>::mapped_type {aka sz::SoundBuffer*}’ in assignment
ResourceManager页: 1: In member function ‘type* sz::ResourceManager::Get(std::string) [with type = sz::Image, std::string = std::basic_string<char>]’:
ResourceManager页: 1:55:2: warning: control reaches end of non-void function [-Wreturn-type]
Compilation failed.
最佳回答

在研究守则和错误信息之后,这是我的想法。

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.

你们必须归还《图像》和《声音》所继承的内容。

问题回答

The templated function have to be completely defined for the compiler to able to use them. That means you have to move the body of those function from the source file to the header file.

<><>Edit>:

你必须检查你如何使用模板类别。 例如,见这一错误信息:

ResourceManager.cpp:50:15: error: cannot convert ‘sz::SoundBuffer*’ to ‘sz::Image*’ in return

Unless sz:SoundBuffer inherits from sz:Image You have a mismatch between categories.

“超时”是通过模版部分专门化完成的。 但这里甚至不需要:

template<typename type> struct Container {
  static std::map<std::string, type*> container;
};
std::map<std::string, sz::Image*>& ImagesContainer = Container<sz::Image>::container;
// etc...
template <class type>
void ResourceManager::AddNew(std::string filePath)
{
  type *item = new type(filePath);
  Container<type>::container[filePath] = item;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签