2

今Cocos2dxを用いて本のサンプルゲームを作成しているのですが、タイトルのエラーがでてしまい。なぜこのようなエラーが出てしまっているのかわからない状況です。

以下のコードのcreateSceneWithLevelの最後のリターン文でエラーが出ます。またメソッドの最初でも

Function cannot return function type 'cocos2d::Scene *()'

というエラーが出てしまいます。
修正方法をご教授ください。

ヘッダーとソースコードをいかに示します。

#include "MainScene.h"

USING_NS_CC;

const Vec2 GRAVITY_ACCELERATION = Vec2(0, -3);
const Vec2 IMPULSE_ACCELERATION = Vec2(0, 180);

Scene* MainScene::createSceneWithLevel(int level)()
{
    // 'scene' is an autorelease object
    auto scene = Scene::createWithPhysics();

    auto world = scene->getPhysicsWorld();

    //重力を設定する
    world->setGravity(GRAVITY_ACCELERATION);

#if COCOS2D_DEBUG > 0

    world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
#endif


    world->setSpeed(6.0);

    auto layer = new MainScene();
    if(layer && layer->initWithLevel(level)){
        layer->autorelease();
    }else{
        CC_SAFE_DELETE(layer);
    }
    scene->addChild(layer);

    //return scene
    return scene;
}

bool MainScene::initWithLevel(int level)
{
    if(!Layer::init())
        return false;

    //ステージを生成
    auto stage = Stage::createWithLevel(level);
    this->addChild(stage);
    this->setStage(stage);

    //タッチした時のリスナを初期化
    auto listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = [this](Touch *touch, Event *event){
        this->setIsPress(true);
        return true;
    };
    listener->onTouchEnded = [this](Touch *touch, Event *event){
        this->setIsPress(false);
    };
    listener->onTouchCancelled = [this](Touch *touch, Event *event){
        this->setIsPress(false);
    };
    //Register event listener
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

    //ぶつかったリスナを定義
    auto contactListener = EventListenerPhysicsContact::create();
    contactListener->onContactBegin = [](PhysicsContact& contact){
        log("hit");
        return true;
    };
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this);

    this->scheduleUpdate();
    return this;
}

MainScene::MainScene()
: _stage(nullptr)
,_isPress(false)
{

}

MainScene::~MainScene()
{
    CC_SAFE_RELEASE_NULL(_stage);
}

void MainScene::update(float dt)
{
    if(this->getIsPress()){
        //プレスされた場合プレイヤの推進力をつける
        _stage->getPlayer()->getPhysicsBody()->applyImpulse(IMPULSE_ACCELERATION);
    }
}
#ifndef __SampleGame_SCENE_H__
#define __SampleGame_SCENE_H__

#include "cocos2d.h"
#include "Stage.h"

class MainScene : public cocos2d::Layer
{
protected:
    MainScene();
    virtual ~MainScene();
    bool init() override;
    bool initWithLevel(int level);
public:
    static cocos2d::Scene* createSceneWithLevel(int level);
    void update(float dt);
    CC_SYNTHESIZE(bool, _isPress, IsPress);
    CC_SYNTHESIZE_RETAIN(Stage *, _stage, Stage);
};

#endif // __SampleGame_SCENE_H__
2
  • 2
    MainScene::createSceneWithLevel(int level)() となっていて、最後に () が付いていますが、これは書き間違いでしょうか…?
    – user9156
    2015年8月27日 5:30
  • 1
    気が付きませんでした(恥) ありがとうございました!! 2015年8月27日 5:45

1 件の回答 1

1

エラー原因は@argusさんコメントの通りMainScene::createSceneWithLevel(int level)()の最後の余計な()です。


以下のコードのcreateSceneWithLevelの最後のリターン文でエラーが出ます。またメソッドの最初でも

Function cannot return function type 'cocos2d::Scene *()'

というエラーが出てしまいます。

このエラーメッセージは、Scene* MainScene::createSceneWithLevel(int level)()がC++コンパイラによって下記のように解釈された結果です。

  • MainSceneクラスのメンバ関数createSceneWithLevelの定義を始める。
  • 同メンバ関数の引数リストとして、int型の引数1個をとる。
  • 同メンバ関数の戻り値型として、「引数0個で戻り値型Scene*を返す」関数型を返す。

メッセージに含まれるcocos2d::Scene *()は、3点目の「引数0個で戻り値型Scene*を返す」関数型に該当します。そして、(関数"ポインタ"型ではない)関数型は値として扱えませんので、Function cannot return function typeという指摘に繋がります。

この奇妙な解釈結果は、C++言語における関数型まわりの分かりにくい構文によるものです。戻り値型部分を別名定義(using or typedef)すると、少しだけ読みやすく書き換えることができます。

using FnType = cocos2d::Scene* ();
// または typedef cocos2d::Scene* FnType();
FnType MainScene::createSceneWithLevel(int level);

余談ですが、一般的に前述のような「関数型」を利用することはあまりなく、必要なのは「関数ポインタ型」というケースが大半でしょう。

using FnPtrType = cocos2d::Scene* (*)();
// または typedef cocos2d::Scene* (*FnPtrType)();

cocos2d::Scene* some_func();
FnPtrType pFn = &some_func;

仮に、本当にこのような関数ポインタ型を返したかった場合は、関数プロトタイプは次のようになります。

Scene* (*MainScene::createSceneWithLevel(int level))();
2
  • 詳しい回答をありがとう御座います。C++を触り始めたばかりなのでよくわかりませんがC++はPythonのように関数を引数に取ることができるので私が間違えたような書き方は使い方によっては文法的に正しいということですか。 2015年8月28日 14:24
  • ややこしいのですが、ここでは「文法的(syntax)には正しいが意味論(semantics)が不適切」というエラーになっています。Pythonだと特に気にしなくてよいのですが、C++では関数型と関数ポインタ型(と関数参照型)が区別されるので、このような事が起きてしまいます。
    – yohjp
    2015年8月31日 2:16

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。