对C ++中构造函数的未定义引用

时间:2013-03-08 10:30:35

标签: android c++ cocos2d-x

我正在创建类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文件的构造函数中调用此方法。我在HelloWorld.h中包含了Levels.h文件,我将其包含在HelloWorld.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_ */

Level.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;     }

Android.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)

2 个答案:

答案 0 :(得分:0)

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

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

与析构函数类似:

Levels::~Levels()
{
    // destructor code here …
}

答案 1 :(得分:0)

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

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

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

相关问题