2013-03-08 188 views
1

我创建类电平的构造,但b口正在收到错误消息进行如下的:未定义参考构造函数C++

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.cpp中的HelloWorld.h中包含了Levels.h文件。

我将不胜感激,如果任何人可以帮助我,因为我是初学者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) 
+0

从'Level.h'中添加'getCachedDataFromFile'的声明。当前信息不够 – uba 2013-03-08 10:33:23

+0

包含标题,其中定义了级别。而你并没有创建Levels的构造函数。 – TheMathemagician 2013-03-08 10:35:08

+0

你正在编译/连接Level.cpp以及包含它的头文件吗? – simonc 2013-03-08 10:35:18

回答

0

你实现构造函数?与析构函数同样

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

:在Levels.cpp添加此

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

我已经以这种方式实现了。 – 2013-03-08 11:27:01

0

你忘了添加Levels.cpp到的SRC文件在你的Android.mk列表。它应该是:

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

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