为什么当我把构造函数定义放在体中而不是头文件中时,我得到了未定义的引用?
Why do I get undefined reference when I place constructor definition in the body but not in the header file?
我遵循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)来帮助解决这个问题,详细信息可以在这里找到:链接
相关文章:
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 在 cpp 文件中隐藏采用模板参数引用的方法
- 如何按文件扩展名引用文件夹中的文件
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常
- 引用文件的适当方法是什么?
- 使用 scope 运算符 (::) 引用另一个文件中的类
- C++/生成文件错误:未定义对"main"的引用
- 不同C++文件中未命名命名空间中的名称可以引用同一个命名事物吗?
- 在字符串中引用 Linux 用户名以打开文件
- .cpp文件中未定义的引用错误
- 安卓工作室 |CPP 文件错误错误: 位图库中对"AndroidBitmap_unlockPixels"的未定义引用
- c++ 中目录中文件的未定义引用错误
- 对模板运算符的未定义引用,其定义位于同一头文件中
- 对"列表"的引用不明确,包括头文件
- 调用函数并使用文件作为引用
- bitbake-grpc交叉编译/配置失败,错误为c-ares::care引用文件/usr/lib/libcares.s
- 如何使用QXmlStreamReader解析包含对其他XML文件的引用的XML文件
- 使用Cygwin C++时出现未定义的引用错误,为什么我的文件没有链接?
- 引用另一个文件夹中的.dll
- C++ LNK2005"已定义"错误 - 文件引用自身