0

在 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 文件中的代码,链接器错误就会消失,因此不太可能是编译源错误。

通过一些搜索,我发现一些人提到此代码的符号可能有些隐藏,我希望了解这里实际发生了什么。

4

1 回答 1

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

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

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

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

于 2013-03-26T13:44:18.717 回答