【cocos2d-x 3.x 学习与应用总结】4: 使用cocos compile编译apk
前言本文总结了使用 cocos compile 基本用法C++11中的强类型枚举(scoped enum)作为hash表键值导致的编译错误对于基本的数据类型已经有了预定义的hash函数: // std::hash
// Defined in <funtional>
template<> struct hash<bool>;
template<> struct hash<char>;
template<> struct hash<signed char>;
template<> struct hash<unsigned char>;
template<> struct hash<char16_t>;
template<> struct hash<char32_t>;
template<> struct hash<wchar_t>;
template<> struct hash<short>;
template<> struct hash<unsigned short>;
template<> struct hash<int>;
template<> struct hash<unsigned int>;
template<> struct hash<long>;
template<> struct hash<long long>;
template<> struct hash<unsigned long>;
template<> struct hash<unsigned long long>;
template<> struct hash<float>;
template<> struct hash<double>;
template<> struct hash<long double>;
template< class T > struct hash<T*>;
我的cocos代码中使用了一个强类型枚举作为键值的 enum class MessageType
{
kMessageTypeChangePage = 0,kMessageTypePushPage,kMessageTypePopPage,kMessageTypeChangeBackground,};
class MessageCenter : public Singleton<MessageCenter> {
// ......
private:
typedef std::vector<Message*> MessageQueue;
MessageQueue messages_;
typedef std::set<PriorityHandler*> HandlerQueue;
typedef std::unordered_map<MessageType,HandlerQueue> HandlerMap;
HandlerMap handlerMap_;
};
其中的HandlerMap类型就是以enum class MessageType来作为键值的hash表,在windows上我使用visual studio 2013编译项目,顺利通过。 编译apk时就出问题了。在使用 In file included from D:/programs/android-ndk-r9d/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/hashtable.h:35:0,from D:/programs/android-ndk-r9d/sources/cxx-stl/gnu-libstdc++/4.8/include/unordered_map:47,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../renderer/CCTexture2D.h:32,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../base/CCProtocols.h:34,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../2d/CCNode.h:34,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../2d/CCScene.h:32,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../base/CCDirector.h:37,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../base/CCAsyncTaskPool.h:29,from D:codes3.9PlayingWithCocos3Dproj.android../cocos2d/cocos/3d/../cocos2d.h:41,from jni/../../Classes/util/cocos_util.h:3,from jni/../../Classes/cocos_include.h:4,from jni/../../Classes/message/MessageCenter.h:4,from jni/../../ClassesmessageMessageCenter.cpp:1:
D:/programs/android-ndk-r9d/sources/cxx-stl/gnu-libstdc++/4.8/include/bits/hashtable_policy.h:1082:53: error: invalid use of incomplete type 'struct std::hash<MessageType>'
using __ebo_h1 = _Hashtable_ebo_helper<1,_H1>;
^
编译器抱怨说 : struct std::hash是不完整类型。它为什么会这样认为,这是因为C++标准中并没有要求对enum class类型的东西,编译器要为其提供默认的hash函数。windows上VC++编译器应该是提供了enum class的hash函数,因此编译能够通过。那么对于gcc编译器,要想解决这个问题就需要自己定义hash函数,针对我的enum class MessageType。 enum class MessageType
{
kMessageTypeChangePage = 0,};
namespace std
{
template <>
struct hash<MessageType>
{
typedef MessageType argument_type;
typedef size_t return_type;
return_type operator() (const argument_type &arg) const
{
return static_cast<return_type>(arg);
}
};
}
为了快点通过编译在android上看到我的游戏测试结果,我定义了一个很简单的hash函数,直接把enum的值转为size_t。 有了这个hash函数,NDK编译通过了并且打包成功。 作者水平有限,对相关知识的理解和总结难免有错误,还望给予指正,非常感谢! 在这里也能看到这篇文章:github博客,CSDN博客,欢迎访问 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |