std::map 的困难
编辑: 数据如下
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你的问题是
itr->second
是一种类型(Resource
,如果你的std::map
typedef 正确),而mDefault
必须是其他的。三元运算符无法处理这两种不同的类型,因此您必须更正代码以确保
?:
运算符的:
部分左侧和右侧的项目都属于相同类型(或兼容类型)。因此,确认这一点,我需要:
Resource
类型的声明T
类型的声明mDefault
成员变量的声明编辑
让我们假设你有类似的东西:
实例化如下:
现在,我需要
mDefault
的类型才能继续。我的猜测:您必须确保您的代码更像是:
当您想要返回
Resource
时,而不是Resource
的shared_ptr。请注意,我添加了
const
关键字/前缀,以与const
返回编辑 2
一致:
所以我想你应该写:
itr->second
是一个智能指针,因此如果想要获取指针对象,只需解引用智能指针即可:*(itr->second)
。至于返回对 mDefault 的引用,这是由函数的返回类型
const T &
指示的,因此您不需要其他任何内容。I guess your problem is that
itr->second
is of one type (Resource
, if yourstd::map
typedef is correct), whilemDefault
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:
Resource
typeT
typemDefault
member variableEdit
Let's assume you have something like:
instanciated like:
Now, I need the type of
mDefault
to continue.My guess: You MUST make sure your code is more like:
As you want to return the
Resource
, not the shared_ptr of theResource
.Note that I added
const
keywords/prefix to be consistent with theconst
returnEdit 2
As you have:
So I guess you should write:
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.