2015-09-10 18 views
6

Ho questo gioco cocos 2dx per IOS e voglio portarlo su Android. Capisco che devo scrivere tutte le mie lezioni su android.mk e l'ho fatto. ma ancora ho questo problemariferimento non definito a 'cocos2d :: UserDefault'

Classes/scenes/itemshop/ItemShop.cpp:144: error: undefined reference to 'cocos2d::UserDefault::GetValueForKeyPlist

questo è semplicemente orribile che non capisco il motivo per cui ho questo errore, ho UserDefault classe dichiarata nel android.mk

EDIT: Ecco il codice :

CCUserDefault.h

/**************************************************************************** 
    Copyright (c) 2010-2012 cocos2d-x.org 
    Copyright (c) 2013-2014 Chukong Technologies Inc. 

    http://www.cocos2d-x.org 

    Permission is hereby granted, free of charge, to any person obtaining a copy 
    of this software and associated documentation files (the "Software"), to deal 
    in the Software without restriction, including without limitation the rights 
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
    copies of the Software, and to permit persons to whom the Software is 
    furnished to do so, subject to the following conditions: 

    The above copyright notice and this permission notice shall be included in 
    all copies or substantial portions of the Software. 

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
    THE SOFTWARE. 
    ****************************************************************************/ 
    #ifndef __SUPPORT_CCUSERDEFAULT_H__ 
    #define __SUPPORT_CCUSERDEFAULT_H__ 

    #include "base/CCPlatformMacros.h" 
    #include <string> 
    #include "base/CCData.h" 

    NS_CC_BEGIN 

    /** 
    * @addtogroup data_storage 
    * @{ 
    */ 

    /** 
    * UserDefault acts as a tiny database. You can save and get base type values by it. 
    * For example, setBoolForKey("played", true) will add a bool value true into the database. 
    * Its key is "played". You can get the value of the key by getBoolForKey("played"). 
    * 
    * It supports the following base types: 
    * bool, int, float, double, string 
    */ 
    class CC_DLL UserDefault 
    { 
    public: 
     // get value methods 

     /** 
     @brief Get bool value by key, if the key doesn't exist, a default value will return. 
     You can set the default value, or it is false. 
     * @js NA 
     */ 
     bool getBoolForKey(const char* pKey); 
     /** 
     * @js NA 
     */ 
     bool getBoolForKey(const char* pKey, bool defaultValue); 
     /** 
     @brief Get integer value by key, if the key doesn't exist, a default value will return. 
     You can set the default value, or it is 0. 
     * @js NA 
     */ 
     int  getIntegerForKey(const char* pKey); 
     /** 
     * @js NA 
     */ 
     int  getIntegerForKey(const char* pKey, int defaultValue); 
     /** 
     @brief Get float value by key, if the key doesn't exist, a default value will return. 
     You can set the default value, or it is 0.0f. 
     * @js NA 
     */ 
     float getFloatForKey(const char* pKey); 
     /** 
     * @js NA 
     */ 
     float getFloatForKey(const char* pKey, float defaultValue); 
     /** 
     @brief Get double value by key, if the key doesn't exist, a default value will return. 
     You can set the default value, or it is 0.0. 
     * @js NA 
     */ 
     double getDoubleForKey(const char* pKey); 
     /** 
     * @js NA 
     */ 
     double getDoubleForKey(const char* pKey, double defaultValue); 
     /** 
     @brief Get string value by key, if the key doesn't exist, a default value will return. 
     You can set the default value, or it is "". 
     * @js NA 
     */ 
     std::string getStringForKey(const char* pKey); 
     /** 
     * @js NA 
     */ 
     std::string getStringForKey(const char* pKey, const std::string & defaultValue); 
     /** 
     @brief Get binary data value by key, if the key doesn't exist, a default value will return. 
     You can set the default value, or it is null. 
     * @js NA 
     * @lua NA 
     */ 
     Data getDataForKey(const char* pKey); 
     /** 
     * @js NA 
     * @lua NA 
     */ 
     Data getDataForKey(const char* pKey, const Data& defaultValue); 

     // set value methods 

     /** 
     @brief Set bool value by key. 
     * @js NA 
     */ 
     void setBoolForKey(const char* pKey, bool value); 
     /** 
     @brief Set integer value by key. 
     * @js NA 
     */ 
     void setIntegerForKey(const char* pKey, int value); 
     /** 
     @brief Set float value by key. 
     * @js NA 
     */ 
     void setFloatForKey(const char* pKey, float value); 
     /** 
     @brief Set double value by key. 
     * @js NA 
     */ 
     void setDoubleForKey(const char* pKey, double value); 
     /** 
     @brief Set string value by key. 
     * @js NA 
     */ 
     void setStringForKey(const char* pKey, const std::string & value); 
     /** 
     @brief Set binary data value by key. 
     * @js NA 
     * @lua NA 
     */ 
     void setDataForKey(const char* pKey, const Data& value); 
     /** 
     @brief Save content to xml file 
     * @js NA 
     */ 
     void flush(); 

     /** returns the singleton 
     * @js NA 
     * @lua NA 
     */ 
     static UserDefault* getInstance(); 
     /** 
     * @js NA 
     */ 
     static void destroyInstance(); 

     /** deprecated. Use getInstace() instead 
     * @js NA 
     * @lua NA 
     */ 
     CC_DEPRECATED_ATTRIBUTE static UserDefault* sharedUserDefault(); 
     /** 
     * @js NA 
     */ 
     CC_DEPRECATED_ATTRIBUTE static void purgeSharedUserDefault(); 
     /** 
     * @js NA 
     */ 
     static const std::string& getXMLFilePath(); 
     /** 
     * @js NA 
     */ 
     static bool isXMLFileExist(); 

     std::string GetValueForKeyPlist(std::string key); 


    // bool searchTextInPlist(std::string text); 

    private: 
     UserDefault(); 
     ~UserDefault(); 

     static bool createXMLFile(); 
     static void initXMLFilePath(); 

     static UserDefault* _userDefault; 
     static std::string _filePath; 
     static bool _isFilePathInitialized; 
    }; 

    // end of data_storage group 
    /// @} 

    NS_CC_END 

    #endif // __SUPPORT_CCUSERDEFAULT_H__ 

CCUserDefault.cpp

/**************************************************************************** 
Copyright (c) 2010-2012 cocos2d-x.org 
Copyright (c) 2013-2014 Chukong Technologies Inc. 

http://www.cocos2d-x.org 

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions: 

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE SOFTWARE. 
****************************************************************************/ 
#include "base/CCUserDefault.h" 
#include "platform/CCCommon.h" 
#include "platform/CCFileUtils.h" 
#include "tinyxml2.h" 
#include "base/base64.h" 
#include "base/ccUtils.h" 

#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_TARGET_PLATFORM != CC_PLATFORM_MAC && CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) 

// root name of xml 
#define USERDEFAULT_ROOT_NAME "userDefaultRoot" 

#define XML_FILE_NAME "UserDefault.xml" 

using namespace std; 

NS_CC_BEGIN 

/** 
* define the functions here because we don't want to 
* export xmlNodePtr and other types in "CCUserDefault.h" 
*/ 

static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLElement** rootNode, tinyxml2::XMLDocument **doc) 
{ 
    tinyxml2::XMLElement* curNode = nullptr; 

    // check the key value 
    if (! pKey) 
    { 
     return nullptr; 
    } 

    do 
    { 
     tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument(); 
     *doc = xmlDoc; 

     std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath()); 

     if (xmlBuffer.empty()) 
     { 
      CCLOG("can not read xml file"); 
      break; 
     } 
     xmlDoc->Parse(xmlBuffer.c_str(), xmlBuffer.size()); 

     // get root node 
     *rootNode = xmlDoc->RootElement(); 
     if (nullptr == *rootNode) 
     { 
      CCLOG("read root node error"); 
      break; 
     } 
     // find the node 
     curNode = (*rootNode)->FirstChildElement(); 
     while (nullptr != curNode) 
     { 
      const char* nodeName = curNode->Value(); 
      if (!strcmp(nodeName, pKey)) 
      { 
       break; 
      } 

      curNode = curNode->NextSiblingElement(); 
     } 
    } while (0); 

    return curNode; 
} 

static void setValueForKey(const char* pKey, const char* pValue) 
{ 
    tinyxml2::XMLElement* rootNode; 
    tinyxml2::XMLDocument* doc; 
    tinyxml2::XMLElement* node; 
    // check the params 
    if (! pKey || ! pValue) 
    { 
     return; 
    } 
    // find the node 
    node = getXMLNodeForKey(pKey, &rootNode, &doc); 
    // if node exist, change the content 
    if (node) 
    { 
     if (node->FirstChild()) 
     { 
      node->FirstChild()->SetValue(pValue); 
     } 
     else 
     { 
      tinyxml2::XMLText* content = doc->NewText(pValue); 
      node->LinkEndChild(content); 
     } 
    } 
    else 
    { 
     if (rootNode) 
     { 
      tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey); 
      rootNode->LinkEndChild(tmpNode); 
      tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue); 
      tmpNode->LinkEndChild(content); 
     } 
    } 

    // save file and free doc 
    if (doc) 
    { 
     doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str()); 
     delete doc; 
    } 
} 

/** 
* implements of UserDefault 
*/ 

UserDefault* UserDefault::_userDefault = nullptr; 
string UserDefault::_filePath = string(""); 
bool UserDefault::_isFilePathInitialized = false; 

UserDefault::~UserDefault() 
{ 
} 

UserDefault::UserDefault() 
{ 
} 

bool UserDefault::getBoolForKey(const char* pKey) 
{ 
return getBoolForKey(pKey, false); 
} 

bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue) 
{ 
    const char* value = nullptr; 
    tinyxml2::XMLElement* rootNode; 
    tinyxml2::XMLDocument* doc; 
    tinyxml2::XMLElement* node; 
    node = getXMLNodeForKey(pKey, &rootNode, &doc); 
    // find the node 
    if (node && node->FirstChild()) 
    { 
     value = (const char*)(node->FirstChild()->Value()); 
    } 

    bool ret = defaultValue; 

    if (value) 
    { 
     ret = (! strcmp(value, "true")); 
    } 

    if (doc) delete doc; 

    return ret; 
} 

int UserDefault::getIntegerForKey(const char* pKey) 
{ 
    return getIntegerForKey(pKey, 0); 
} 

int UserDefault::getIntegerForKey(const char* pKey, int defaultValue) 
{ 
    const char* value = nullptr; 
    tinyxml2::XMLElement* rootNode; 
    tinyxml2::XMLDocument* doc; 
    tinyxml2::XMLElement* node; 
    node = getXMLNodeForKey(pKey, &rootNode, &doc); 
    // find the node 
    if (node && node->FirstChild()) 
    { 
     value = (const char*)(node->FirstChild()->Value()); 
    } 

    int ret = defaultValue; 

    if (value) 
    { 
     ret = atoi(value); 
    } 

    if(doc) 
    { 
     delete doc; 
    } 


    return ret; 
} 

float UserDefault::getFloatForKey(const char* pKey) 
{ 
    return getFloatForKey(pKey, 0.0f); 
} 

float UserDefault::getFloatForKey(const char* pKey, float defaultValue) 
{ 
    float ret = (float)getDoubleForKey(pKey, (double)defaultValue); 

    return ret; 
} 

double UserDefault::getDoubleForKey(const char* pKey) 
{ 
    return getDoubleForKey(pKey, 0.0); 
} 

double UserDefault::getDoubleForKey(const char* pKey, double defaultValue) 
{ 
    const char* value = nullptr; 
    tinyxml2::XMLElement* rootNode; 
    tinyxml2::XMLDocument* doc; 
    tinyxml2::XMLElement* node; 
    node = getXMLNodeForKey(pKey, &rootNode, &doc); 
    // find the node 
    if (node && node->FirstChild()) 
    { 
     value = (const char*)(node->FirstChild()->Value()); 
    } 

    double ret = defaultValue; 

    if (value) 
    { 
     ret = utils::atof(value); 
    } 

    if (doc) delete doc; 

    return ret; 
} 

std::string UserDefault::getStringForKey(const char* pKey) 
{ 
    return getStringForKey(pKey, ""); 
} 

string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue) 
{ 
    const char* value = nullptr; 
    tinyxml2::XMLElement* rootNode; 
    tinyxml2::XMLDocument* doc; 
    tinyxml2::XMLElement* node; 
    node = getXMLNodeForKey(pKey, &rootNode, &doc); 
    // find the node 
    if (node && node->FirstChild()) 
    { 
     value = (const char*)(node->FirstChild()->Value()); 
    } 

    string ret = defaultValue; 

    if (value) 
    { 
     ret = string(value); 
    } 

    if (doc) delete doc; 

    return ret; 
} 

Data UserDefault::getDataForKey(const char* pKey) 
{ 
    return getDataForKey(pKey, Data::Null); 
} 

Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue) 
{ 
    const char* encodedData = nullptr; 
    tinyxml2::XMLElement* rootNode; 
    tinyxml2::XMLDocument* doc; 
    tinyxml2::XMLElement* node; 
    node = getXMLNodeForKey(pKey, &rootNode, &doc); 
    // find the node 
    if (node && node->FirstChild()) 
    { 
     encodedData = (const char*)(node->FirstChild()->Value()); 
    } 

    Data ret = defaultValue; 

    if (encodedData) 
    { 
     unsigned char * decodedData = nullptr; 
     int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData); 

     if (decodedData) { 
      ret.fastSet(decodedData, decodedDataLen); 
     } 
    } 

    if (doc) delete doc; 

    return ret;  
} 


void UserDefault::setBoolForKey(const char* pKey, bool value) 
{ 
    // save bool value as string 

    if (true == value) 
    { 
     setStringForKey(pKey, "true"); 
    } 
    else 
    { 
     setStringForKey(pKey, "false"); 
    } 
} 

void UserDefault::setIntegerForKey(const char* pKey, int value) 
{ 
    // check key 
    if (! pKey) 
    { 
     return; 
    } 

    // format the value 
    char tmp[50]; 
    memset(tmp, 0, 50); 
    sprintf(tmp, "%d", value); 

    setValueForKey(pKey, tmp); 
} 

void UserDefault::setFloatForKey(const char* pKey, float value) 
{ 
    setDoubleForKey(pKey, value); 
} 

void UserDefault::setDoubleForKey(const char* pKey, double value) 
{ 
    // check key 
    if (! pKey) 
    { 
     return; 
    } 

    // format the value 
    char tmp[50]; 
    memset(tmp, 0, 50); 
    sprintf(tmp, "%f", value); 

    setValueForKey(pKey, tmp); 
} 

void UserDefault::setStringForKey(const char* pKey, const std::string & value) 
{ 
    // check key 
    if (! pKey) 
    { 
     return; 
    } 

    setValueForKey(pKey, value.c_str()); 
} 

void UserDefault::setDataForKey(const char* pKey, const Data& value) { 
    // check key 
    if (! pKey) 
    { 
     return; 
    } 

    char *encodedData = 0; 

    base64Encode(value.getBytes(), static_cast<unsigned int>(value.getSize()), &encodedData); 

    setValueForKey(pKey, encodedData); 

    if (encodedData) 
     free(encodedData); 
} 

UserDefault* UserDefault::getInstance() 
{ 
    initXMLFilePath(); 

    // only create xml file one time 
    // the file exists after the program exit 
    if ((! isXMLFileExist()) && (! createXMLFile())) 
    { 
     return nullptr; 
    } 

    if (! _userDefault) 
    { 
     _userDefault = new UserDefault(); 
    } 

    return _userDefault; 
} 

void UserDefault::destroyInstance() 
{ 
    CC_SAFE_DELETE(_userDefault); 
} 

// XXX: deprecated 
UserDefault* UserDefault::sharedUserDefault() 
{ 
    return UserDefault::getInstance(); 
} 

// XXX: deprecated 
void UserDefault::purgeSharedUserDefault() 
{ 
    return UserDefault::destroyInstance(); 
} 

bool UserDefault::isXMLFileExist() 
{ 
    FILE *fp = fopen(_filePath.c_str(), "r"); 
    bool bRet = false; 

    if (fp) 
    { 
     bRet = true; 
     fclose(fp); 
    } 

    return bRet; 
} 
std::string UserDefault::GetValueForKeyPlist(std::string key){ 
    return "test"; 
} 
void UserDefault::initXMLFilePath() 
{ 
    if (! _isFilePathInitialized) 
    { 
     _filePath += FileUtils::getInstance()->getWritablePath() + XML_FILE_NAME; 
     _isFilePathInitialized = true; 
    }  
} 

// create new xml file 
bool UserDefault::createXMLFile() 
{ 
    bool bRet = false; 
    tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument(); 
    if (nullptr==pDoc) 
    { 
     return false; 
    } 
    tinyxml2::XMLDeclaration *pDeclaration = pDoc->NewDeclaration(nullptr); 
    if (nullptr==pDeclaration) 
    { 
     return false; 
    } 
    pDoc->LinkEndChild(pDeclaration); 
    tinyxml2::XMLElement *pRootEle = pDoc->NewElement(USERDEFAULT_ROOT_NAME); 
    if (nullptr==pRootEle) 
    { 
     return false; 
    } 
    pDoc->LinkEndChild(pRootEle); 
    bRet = tinyxml2::XML_SUCCESS == pDoc->SaveFile(_filePath.c_str()); 

    if(pDoc) 
    { 
     delete pDoc; 
    } 

    return bRet; 
} 

const string& UserDefault::getXMLFilePath() 
{ 
    return _filePath; 
} 

void UserDefault::flush() 
{ 
} 

NS_CC_END 

#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_IOS && CC_PLATFORM != CC_PLATFORM_ANDROID) 

fine qui ci sono alcune chiamate che faccio:

std::string textid = itemname + "_name"; 
std::string word = UserDefault::getInstance()->GetValueForKeyPlist(textid); 
std::string word = UserDefault::getInstance()->GetValueForKeyPlist(node.child("desc").attribute("value").as_string()); 
std::string word = UserDefault::getInstance()->GetValueForKeyPlist(itemname+"_desc"); 

ad ognuna di queste linee ottengo undefined reference to 'cocos2d :: :: UserDefault GetValueForKeyPlist (std :: string)'

mia struttura cocos2dx gioco di codice è come questo

/MYGAME/Classes 
/MYGAME/cocos2d/cocos 
/MYGAME/ml 

ho un file android.mk nella cartella jni e in/MYGAME/cocos2d/cocos, forse è un problema di collegamento, ma se si trattava di un problema di collegamento, perché non ottengo riferimenti non definiti quando si chiamano altre funzioni dalla classe di UserDefault.

Modifica: Problema risolto. C'era un IF nella parte superiore della classe, quindi questa classe si sarebbe compilata solo su piattaforma IOS. ora il mio metodo funziona.

+0

E 'possibile mostrarci un codice? – Gombat

+0

La definizione di 'cocos2d :: UserDefault :: GetValueForKeyPlist' nello stesso progetto o in una libreria al di fuori del progetto? – Gombat

+0

è al di fuori della cartella Classes. Ottengo questo problema quando aggiungo una funzione a questa classe, tutte le altre funzioni "UserDefault" funzionano, ma questa "GetValueForKeyPlist" non lo fa. Ho realizzato il prototipo nel file di intestazione e poi implementato la funzione in .cpp. cosa manca qui? perché ottengo un riferimento indefinito :( – Heixss

risposta

-1

problema risolto. c'era un IF nella parte superiore della classe, quindi questa classe si sarebbe compilata solo su piattaforma IOS. ora il mio metodo funziona.

2

Se il codice di questa classe è nel progetto, significa che esiste una dichiarazione ma nessuna definizione di cocos2d::UserDefault::GetValueForKeyPlist.

Forse la dichiarazione e la definizione differiscono in qualche modo. devono avere lo stesso tipo di ritorno e devono accettare gli stessi argomenti. Entrambi devono essere const o non const, ecc.

Se il codice di questa classe si trova in una libreria, è necessario collegare la libreria al progetto eseguibile per consentirgli di trovare la definizione nella libreria.

+0

forse la definizione è diversa. – Gombat

+0

Non capisco questo è nella libreria cocos2dx – Heixss

+0

Devi collegare la libreria cocos2dx a il tuo progetto binario – Gombat

Problemi correlati