std::map 的困难

发布于 2024-12-28 11:07:11 字数 833 浏览 3 评论 0原文

编辑: 数据如下

typedef std::shared_ptr<T> Resource;
typedef std::map<std::string, Resource> ResourceMap;

这是函数

const T& Get(const std::string& key)
{
    ResourceMap::iterator itr = mResources.find(key);
    return (itr != mResources.end()) ? itr->second : mDefault;

}

错误:

Error   1   error C2446: ':' : no conversion from 'sf::Texture' to 'std::tr1::shared_ptr<_Ty>'  d:\sanity\trunk\client\src\assetmanager.h   28

我也在创建一个这样的对象:

AssetManager<sf::Texture> imgManager;

好的,抱歉缺少信息,我很着急。

剩下的唯一问题是:

return (itr != mResources.end()) ? itr->second.get() : mDefault;

get() 返回原始指针,我需要返回对 shared_ptr 所指向内容的引用。

我该怎么做呢?

Edit:
The data is as follows

typedef std::shared_ptr<T> Resource;
typedef std::map<std::string, Resource> ResourceMap;

This is the function

const T& Get(const std::string& key)
{
    ResourceMap::iterator itr = mResources.find(key);
    return (itr != mResources.end()) ? itr->second : mDefault;

}

Error:

Error   1   error C2446: ':' : no conversion from 'sf::Texture' to 'std::tr1::shared_ptr<_Ty>'  d:\sanity\trunk\client\src\assetmanager.h   28

Also I'm creating an object like that :

AssetManager<sf::Texture> imgManager;

Okay, sorry for missing information, I was in a rush.

The only problem left is :

return (itr != mResources.end()) ? itr->second.get() : mDefault;

get() returns raw pointer and I need to return reference to what the shared_ptr is pointing.

How should I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘑菇王子 2025-01-04 11:07:11
Error   1   error C2446: ':' : no conversion from 'sf::Texture' to
'std::tr1::shared_ptr<_Ty>' d:\sanity\trunk\client\src\assetmanager.h 28

我猜你的问题是 itr->second 是一种类型(Resource,如果你的 std::map typedef 正确),而mDefault必须是其他的。

三元运算符无法处理这两种不同的类型,因此您必须更正代码以确保 ?: 运算符的 : 部分左侧和右侧的项目都属于相同类型(或兼容类型)。

因此,确认这一点,我需要:

  • Resource 类型的声明
  • T 类型的声明
  • mDefault 成员变量的声明

编辑

让我们假设你有类似的东西:

typedef std::shared_ptr<T> Resource;
typedef std::map<std::string, Resource> ResourceMap;

template <typename T>
class AssetManager
{
    const T& Get(const std::string& key)
    {
        ResourceMap::iterator itr = mResources.find(key);
        return (itr != mResources.end()) ? itr->second : mDefault;
    }

    ResourceMap mResources ;
    ??? mDefault ;

    // etc.
} ;

实例化如下:

AssetManager<sf::Texture> imgManager;

现在,我需要 mDefault 的类型才能继续。

我的猜测:您必须确保您的代码更像是:

    const T& Get(const std::string& key) const
    {
        ResourceMap::const_iterator itr = mResources.find(key);
        return (itr != mResources.end()) ? *(itr->second) : *(mDefault);
    }

    ResourceMap mResources ;
    Resource mDefault ;

当您想要返回Resource时,而不是Resource的shared_ptr。

请注意,我添加了 const 关键字/前缀,以与 const 返回

编辑 2

一致:

    ResourceMap mResources ;
    T mDefault ;

所以我想你应该写:

    const T& Get(const std::string& key) const
    {
        ResourceMap::const_iterator itr = mResources.find(key);
        return (itr != mResources.end()) ? *(itr->second) : mDefault;
    }

itr->second 是一个智能指针,因此如果想要获取指针对象,只需解引用智能指针即可:*(itr->second)

至于返回对 mDefault 的引用,这是由函数的返回类型 const T & 指示的,因此您不需要其他任何内容。

Error   1   error C2446: ':' : no conversion from 'sf::Texture' to
'std::tr1::shared_ptr<_Ty>' d:\sanity\trunk\client\src\assetmanager.h 28

I guess your problem is that itr->second is of one type (Resource, if your std::map typedef is correct), while mDefault must be something other.

The ternary operator cannot handle the two different types, so you must correct your code to be sure both items left and right of the : part of the ?: operator are of the same type (or compatible ones).

So confirm this, I need:

  • the declaration of the Resource type
  • the declaration of the T type
  • the declaration of the mDefault member variable

Edit

Let's assume you have something like:

typedef std::shared_ptr<T> Resource;
typedef std::map<std::string, Resource> ResourceMap;

template <typename T>
class AssetManager
{
    const T& Get(const std::string& key)
    {
        ResourceMap::iterator itr = mResources.find(key);
        return (itr != mResources.end()) ? itr->second : mDefault;
    }

    ResourceMap mResources ;
    ??? mDefault ;

    // etc.
} ;

instanciated like:

AssetManager<sf::Texture> imgManager;

Now, I need the type of mDefault to continue.

My guess: You MUST make sure your code is more like:

    const T& Get(const std::string& key) const
    {
        ResourceMap::const_iterator itr = mResources.find(key);
        return (itr != mResources.end()) ? *(itr->second) : *(mDefault);
    }

    ResourceMap mResources ;
    Resource mDefault ;

As you want to return the Resource, not the shared_ptr of the Resource.

Note that I added const keywords/prefix to be consistent with the const return

Edit 2

As you have:

    ResourceMap mResources ;
    T mDefault ;

So I guess you should write:

    const T& Get(const std::string& key) const
    {
        ResourceMap::const_iterator itr = mResources.find(key);
        return (itr != mResources.end()) ? *(itr->second) : mDefault;
    }

itr->second is a smart pointer, so if you want to get the pointer object, you simply need to dereference the smart pointer: *(itr->second).

As for returning a reference to mDefault, this is indicated by the function's return type const T &, so you don't need anything more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文