1

我正在创建类 Level 的构造函数,如下所示,但是我收到一条错误消息:

D:\downloads\cocos2d-2.0-x-2.0.4\cocos2d-2.0-x-2.0.4\armadillo\proj.android/jni/../../Classes/HelloWorldScene.cpp:43: undefined reference to `Levels::Levels()'
D:\downloads\cocos2d-2.0-x-2.0.4\cocos2d-2.0-x-2.0.4\armadillo\proj.android/jni/../../Classes/HelloWorldScene.cpp:44: undefined reference to `Levels::getCachedDataFromFile(std::string)'

代码:

bool HelloWorld::init()
{
  if ( !CCLayer::init() )
  {
    return false;
  }
  _levels = new Levels();
  _levels->getCachedDataFromFile("\mnt\sdcard\levels.json");
  return true;
}

我在 HelloWorld.cpp 文件的构造函数中调用此方法。我已将 Levels.h 文件包含在 HelloWorld.h 中,该文件已包含在 HelloWorld.cpp 中。

如果有人能帮助我,我将不胜感激,因为我是 cpp 的初学者。

我已经在头文件中包含了方法和构造函数,你可以在下面的代码中检查它:

#include "Box2d.h"
#include "cocos2d.h"

using namespace cocos2d;

#ifndef LEVELS_H_
#define LEVELS_H_

class Levels: public b2ContactListener {

public:

Levels();
~Levels();

void BeginContact(b2Contact *contact);
void EndContact(b2Contact *contact);
void preSolve(b2Contact* contact, const b2Manifold* oldManifold);
void postSolve(b2Contact* contact, const b2ContactImpulse* impulse);
void getCachedDataFromFile(string filePath);


private:

//  const string LEVEL_FILE_NAME = "levels.json";

};

#endif /* LEVELS_H_ */

级别.cpp

#include "Levels.h"
#include <android/log.h>


#define  LOG_TAG    "levels"
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)

Levels::Levels() {

}

Levels::~Levels() {

}

void Levels::BeginContact(b2Contact *contact) {

}

void Levels::EndContact(b2Contact *contact) {

}

void Levels::getCachedDataFromFile(string filePath) {
unsigned long filesize = 0;
unsigned char* fileData = NULL;
std::string content, fullPath;
int i =1;
fullPath = CCFileUtils::sharedFileUtils()-    >       fullPathFromRelativePath(filePath.c_str());

fileData = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(),
        "r", &filesize);
content.append((char*) fileData);
LOGD(content.c_str());
 // if (languagesDocument.Parse < 0 > (content.c_str()).HasParseError()) {
 //     LOGD(languagesDocument.GetParseError());
 ////       CCLog(languagesDocument.GetParseError());
//  }

// 返回 NULL; }

安卓.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
#OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include ../../../projects/android-opencv/sdk/native/jni/OpenCV.mk

LOCAL_MODULE := game_shared

LOCAL_MODULE_FILENAME := libgame

LOCAL_SRC_FILES := hellocpp/main.cpp \
               ../../Classes/AppDelegate.cpp \
               ../../Classes/HelloWorldScene.cpp
#Required for android log from jni code
LOCAL_LDLIBS +=  -llog -ldl

#Add path to OpenCV's header files
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../Classes     \
$(LOCAL_PATH)/../../libs/Box2d \
../../../projects/android-opencv/sdk/native/jni/include/

LOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_static cocosdenshion_static          c cocos_extension_static box2d_static

include $(BUILD_SHARED_LIBRARY)

$(call import-module,CocosDenshion/android) \
$(call import-module,cocos2dx) \
$(call import-module,extensions) \ 
$(call import-module,Box2D)
4

2 回答 2

0

你实现了构造函数吗?在 Levels.cpp 中添加:

Levels::Levels()
{
    // constructor code here …
}

与析构函数类似:

Levels::~Levels()
{
    // destructor code here …
}
于 2013-03-08T11:14:11.870 回答
0

您忘记添加Levels.cpp到 Android.mk 中的 src 文件列表。它应该是:

LOCAL_SRC_FILES := hellocpp/main.cpp \
               ../../Classes/AppDelegate.cpp \
               ../../Classes/HelloWorldScene.cpp \
               ../../Classes/Levels.cpp

您稍后创建的任何具有类的文件也应添加到列表中。

于 2013-05-02T23:29:06.163 回答