为什么当我把构造函数定义放在体中而不是头文件中时,我得到了未定义的引用?

Why do I get undefined reference when I place constructor definition in the body but not in the header file?

本文关键字:文件 引用 未定义 定义 构造函数 为什么      更新时间:2023-10-16

我遵循http://discuss.cocos2d-x.org/t/cocos3-0-tutorial-game-catchme/14258/9的教程,使用确切的说明,即没有指定到cocos控制台的包(尝试与包相同的效果),并使用new->Other->C/c++ -> class来创建头和主体(而不是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();的未定义引用

但是如果我把函数的定义从代码体中移除并放在header中:

CatchMe() {}

一切似乎都很美好。

这发生在eclipse和从命令行构建时。

我很想使用默认构造函数这样的术语来澄清,但是自从我上次编码以来已经太长时间了,而且它发生在我添加的所有方法中…奇怪的

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

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