当我将构造函数定义放在正文中但不在头文件中时,为什么会得到未定义的引用?

时间:2014-07-03 08:53:43

标签: c++ android-ndk cocos2d-x

我正在使用http://discuss.cocos2d-x.org/t/cocos3-0-tutorial-game-catchme/14258/9上的教程,使用确切的说明,即没有为cocos控制台指定包(尝试使用相同的包),并使用new-> Other-> C / C ++ - >用于创建标题和正文的类(而不是2xnew->文件)保持简单我只是得到:

HelloWorldScene.h

    #ifndef __HELLOWORLD_SCENE_H__
    #define __HELLOWORLD_SCENE_H__
    #include "cocos2d.h"        
    class HelloWorld : public cocos2d::Layer
    {
    protected:
        CatchMe* Game;
    public:
        static cocos2d::Scene* createScene();
        virtual bool init();  
        void menuCloseCallback(cocos2d::Ref* pSender);
        CREATE_FUNC(HelloWorld);
    };       
    #endif // __HELLOWORLD_SCENE_H__

HelloWorld.cpp

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    auto menu = Menu::create(closeItem, NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu, 1);
    Game = new CatchMe();    
    auto label = LabelTTF::create("Hello World", "Arial", 24);
    label->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + visibleSize.height - label->getContentSize().height));
    this->addChild(label, 1);
    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(sprite, 0);    
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

CatchMe.h

#ifndef CATCHME_H_
#define CATCHME_H_

class CatchMe {
public:
    CatchMe();
    ~CatchMe();
};

#endif /* CATCHME_H_ */

CatchMe.cpp

#include "CatchMe.h"

CatchMe::CatchMe() {
}

CatchMe::~CatchMe() {
}

我得到CatchMe::CatchMe();

的未定义引用

但是,如果我从正文中删除所述函数的定义并将其放在标题中:

CatchMe() {}

一切都很好看,似乎......

这可以通过eclipse和命令行构建来实现。

我很想使用像默认的cunstructor这样的术语来澄清,但是自从我上次编码以来已经太久了,加上它发生在我添加的所有方法中......奇怪

1 个答案:

答案 0 :(得分:3)

根据我在问题下的评论 - 解决方案是在CatchMe.cpp中添加Android.mk以构建订单。该文件可以在名为jni的文件夹中的项目树中找到。应该在那里添加所有.cpp个文件。

前段时间我创建了一个小脚本(mac / linux)来帮助解决这个问题,详情可以在这里找到:link