【发布时间】:2019-04-10 18:06:20
【问题描述】:
我正在使用 cocos2d-x 在 mac 和 windows 上制作可玩的游戏。
我先用Xcode写了代码,可以在mac上运行。
当我将它带到 Windows 并尝试在 Visual Studio 2017 中构建时遇到错误。
NRZNotification.h
#include "cocos2d.h"
class NRZNotification : public cocos2d::Ref
{
protected:
std::string _name;
cocos2d::Ref* _sender;
...
cocos2d::ValueMap _valueMap;
cocos2d::Map<std::string, cocos2d::Ref*> _objectMap;
public:
const std::string& getName(){return _name;}
cocos2d::Ref* getSender(){return _sender;}
NRZNotification();
virtual ~NRZNotification();
static NRZNotification* create(const std::string& name, Ref* sender);
bool init(const std::string& name, Ref* sender);
...
template <typename T,
typename std::enable_if<!std::is_convertible<T, cocos2d::Ref*>::value,
std::nullptr_t>::type = nullptr>
inline T getValue(const std::string& key)
{
//CCLOG("%s", __PRETTY_FUNCTION__);
return 0;
}
template <typename T,
typename std::enable_if<std::is_convertible<T, cocos2d::Ref*>::value,
std::nullptr_t>::type = nullptr>
inline T getValue(const std::string& key)
{
//CCLOG("%s", __PRETTY_FUNCTION__);
return dynamic_cast<T>(_objectMap.at(key));
}
};
#include "NRZNotification_Private.h"
NRZNotification_Private.h
#include "NRZNotification.h"
...
#pragma mark - get value
template <>
inline int NRZNotification::getValue<int,nullptr>(const std::string& key)
{
if (_valueMap.find(key) == _valueMap.end()) {
return 0;
} else {
return _valueMap.at(key).asInt();
}
}
template <>
inline float NRZNotification::getValue(const std::string& key)
{
if (_valueMap.find(key) == _valueMap.end()) {
return 0.0f;
} else {
return _valueMap.at(key).asFloat();
}
}
template <>
inline double NRZNotification::getValue(const std::string& key)
{
if (_valueMap.find(key) == _valueMap.end()) {
return 0.0;
} else {
return _valueMap.at(key).asDouble();
}
}
...
这些代码在 mac 上运行成功,但在 Visual Studio 2017 中,调用 getValue() 会出现“无法显式专门化”的错误。
getValue()是一个函数模板,根据返回值是否是cocos2d::Ref的子类来划分实现。
此外,对 int、float、string 等进行了专门化。
我应该如何修复这个代码?
我使用的是 cocos2d-x 3.17.1。
谢谢。
【问题讨论】:
-
显示编译错误。
标签: c++ visual-studio cocos2d-x sfinae