在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文件中的代码,链接器错误就会消失,因此不太可能是编译源错误。
通过一些搜索,我发现有一些人引用了这段代码的符号可能有些隐藏,我希望了解这里实际发生了什么。
答案 0 :(得分:4)
static SceneModel* sceneModel;
static SceneView* sceneViewLayer;
static SceneController* sceneControllerLayer;
这些都是非定义的声明,因为它们是static
。您还需要为这些成员提供定义:
SceneModel* SceneView::sceneModel;
SceneView* SceneView::sceneViewLayer;
SceneController* SceneView::sceneControllerLayer;
确保这些只在一个翻译单元中(可能在SceneView.cpp
中),否则您将违反一个定义规则。