gpt4 book ai didi

c++ - 当我将构造函数定义放在主体而不是头文件中时,为什么会得到 undefined reference ?

转载 作者:行者123 更新时间:2023-11-30 02:45:20 25 4
gpt4 key购买 nike

我正在学习 http://discuss.cocos2d-x.org/t/cocos3-0-tutorial-game-catchme/14258/9 上的教程,使用确切的指令,即没有指定包到 cocos 控制台(尝试使用包达到相同的效果),并使用 new->Other->C/C++->Class 来创建标题和正文(而不是 2xnew->files)保持事情很简单,我只是得到:

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__

Hello World .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(); 的 undefined reference

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

CatchMe() {}

似乎一切都是桃色的..

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

我很想使用 default cunstructor 之类的术语来澄清,但自从我上次编码以来已经太久了,而且它发生在我添加的所有方法中......奇怪

最佳答案

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

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

关于c++ - 当我将构造函数定义放在主体而不是头文件中时,为什么会得到 undefined reference ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24549186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com