Search code examples
c++xcodec++11cocos2d-x

no viable overloaded '=' error in Xcode (Eclipse not showing errors)


I have tried to make a game using Cocos2dx 3.4 and Everything works fine in Android (using Eclipse) so I wanted to try it out on Xcode for iOS to see if it will work there i imported it into Xcode but when i try to compile it i get this error no viable overloaded '=' at listener1->onTouchEnded = [=](Touch* touch, Event* event).

This code is from Cocos2d wiki EventDispatcher_Mechanism http://www.cocos2d-x.org/wiki/EventDispatcher_Mechanism

My code:

auto listener1 = EventListenerTouchOneByOne::create();

listener1->setSwallowTouches(true);


listener1->onTouchBegan = [](Touch* touch, Event* event) {  
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        Point locationInNode = target->convertToNodeSpace(touch->getLocation());
        Size s = target->getContentSize();
        Rect rect = Rect(0, 0, s.width, s.height);


        if (rect.containsPoint(locationInNode))
        {

            target->setOpacity(180);
            return true;
        }
        return false;
    };



listener1->onTouchEnded = [=](Touch* touch, Event* event) {
    auto target = static_cast<Sprite*>(event->getCurrentTarget());

    target->setOpacity(255);

        if (target == abc)
        {
            auto sceneABC = ABCfirst::createScene(mute);
            Director::getInstance()->replaceScene(sceneABC);
            return true;
        }

        if (target == n123)
        {
            auto sceneNumbers = Numbers::createScene(mute);
            Director::getInstance()->replaceScene(sceneNumbers);
            return true;
        }

        if (target == lgame)
        {
            auto scenePWL = PWL::createScene(mute);
            Director::getInstance()->replaceScene(scenePWL);
            return true;
        }

        if (target == ngame)
        {
            auto scenePWN = PWN::createScene(mute);
            Director::getInstance()->replaceScene(scenePWN);
            return true;
        }

        if (target == info)
        {
            auto sceneAbout = About::createScene();
            Director::getInstance()->replaceScene(sceneAbout);
            return true;
        }



    };

_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, abc);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),
        n123);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),
        lgame);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),
        ngame);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(),
            info);

I went trough couple of pages where people had similar problems but it didnt solve my problem.Compiler is set to c++11 and onTouchBegan is bool rest are void (i have made the project with the pyhton script).

And this is my first time using xCode so it might be i forgot something. Thank you in advance


Solution

  • I find it helps to explicitly specify the lambda's return type:

    [=](Touch* touch, Event* event) -> void { ... }
    

    This way the compiler will help you get rid of the stray return statements in there. You're currently trying to assign a lambda with signature bool(Touch*,Event*) to an std::function<void(Touch*,Event*)>.