gpt4 book ai didi

c++ - 这段代码如何对链接器隐藏符号?

转载 作者:行者123 更新时间:2023-11-27 23:17:15 36 4
gpt4 key购买 nike

在 SceneView.h 中:

#ifndef ProtoAlpha_SceneView_h
#define ProtoAlpha_SceneView_h

#include "cocos2d.h"
#include "SceneModel.h"
#include "SceneController.h"
#include "CharacterModel.h"
#include "CharacterDerived.h"

USING_NS_CC;

class SceneView : public CCLayer {
private:
static SceneModel* sceneModel;
static SceneView* sceneViewLayer;
static SceneController* sceneControllerLayer;
CharacterModel* t1;
CharacterModel* t2;
CCSprite* t1Sprite;

// scheduled Update
void update(float dt);

在 SceneView.ccp 中:

CCScene* SceneView::scene() {
// 'scene' is an autorelease object
CCScene* scene = CCScene::create();

// 'layer' is an autorelease object
sceneModel = new SceneModel;
sceneViewLayer = SceneView::create();
sceneControllerLayer = new SceneController(*sceneModel->buffer);

// add layer as a child to scene
scene->addChild(sceneViewLayer);
scene->addChild(sceneControllerLayer);

// return the scene
return scene;
}

错误:

Undefined symbols for architecture i386:
"SceneView::sceneModel", referenced from:
SceneView::scene() in SceneView.o
SceneView::update(float) in SceneView.o
"SceneView::sceneViewLayer", referenced from:
SceneView::scene() in SceneView.o
"SceneView::sceneControllerLayer", referenced from:
SceneView::scene() in SceneView.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

如果我注释掉 .ccp 文件中的代码,链接器错误就会消失,因此不太可能是编译源错误。

通过一些搜索,我发现一些人提到这段代码的符号可能是如何隐藏的,我想了解这里到底发生了什么。

最佳答案

static SceneModel* sceneModel;
static SceneView* sceneViewLayer;
static SceneController* sceneControllerLayer;

这些都是不是定义的声明,因为它们是static。您还需要为这些成员提供定义:

SceneModel* SceneView::sceneModel;
SceneView* SceneView::sceneViewLayer;
SceneController* SceneView::sceneControllerLayer;

确保它们仅在一个翻译单元中(可能在 SceneView.cpp 中),否则您将违反单一定义规则。

关于c++ - 这段代码如何对链接器隐藏符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15638490/

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