c++ template problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<typename T> void ResourceManager<T>::load(const std::string& filename)
{
    typename std::vector<std::pair<std::string,T>>::iterator iter = getIterator(filename); // i get error here
    if (iter == resources.end())
    {
        resources.emplace_back(filename,T());
        if (resources.back().second.loadFromFile(filename))
        {
            std::cout << "loaded!";
        }
    }
}
template<typename T> const T& ResourceManager<T>::get(const std::string& filename)
{
    typename std::vector<std::pair<std::string,T>>::iterator iter = getIterator(filename); // no error ( why? when it errors on other one )
    if (iter == resources.end())
    {
        load(filename);
        return resources[iter].second;
    }
    return resources.back().second;
}


as youve seen in the comments, i get a strange error at strange place


here is the error:
https://puu.sh/sWMQt/352d7eff1c.png


the error message says that getIterator has an error: it attempts to create std::vector<std::string, T> (as an argument to some lambda, but it doesn't matter why, vector expects T to be an allocator, and sf::Texture isn't one)
@Cubbi but why does line 15 works?

1
2
ResourceManager<sf::Texture>::getResourceManagerInstance().load("test.png");

this is how i create an instance for this template
There is not enough code shown to answer that question. Perhaps you did not attempt to make a call to that get() function (if you don't use a member of a template it is not compiled)

In any case, the error message is about getIterator, specifically line 49 of "zde\resour..."
so how do i fix this one?

vector expects T to be an allocator

what you mean by this?


heres the getIterator() function

1
2
3
4
5
template<typename T> typename std::vector<std::pair<std::string,T>>::iterator ResourceManager<T>::getIterator(const std::string& filename)
{
    return std::find_if(resources.begin(),resources.end(),
                        [&filename](const std::vector<std::string,T>& item){ return item.first == filename; });
}
Last edited on
this const std::vector<std::string,T>& item ifails to compile because your T is not an allocator. I am guesing you meant const std::pair<std::string,T>& item -- or just use const auto& item if it's C++14
Last edited on
@Cubbi

oh shiit! i didnt notice it lol

thanks for that.
Topic archived. No new replies allowed.