Question

I'll show my code first then explain my issue:

std::vector<std::unique_ptr<SGUIObject> > m_objects;

const std::unique_ptr<SGUIObject>& SGUIManager::getObject(const std::string& object_name)
    {
        for (auto const& iter : m_objects)
        {
            if (iter.get()->getObjectName() == object_name)
                return iter;
        }
    }

//SButton is derived from SGUIObject
//m_clicked is a boolean member in SButton (private)

//isClicked is a public member method of SButton
const bool isClicked() const { return m_clicked; }

if (dynamic_cast<SButton>(SSceneManager::getGUIManager().getObject("testbutton").isClicked()))
            std::cout << "Clicked!" << std::endl;

I just copy pasted from several different files, so it looks weird when all put together. Anyways, what I'm trying to do is downcast from a SGUIObject to a SButton and call isClicked() in an if/else loop. When I do my current code, Code::Blocks gives me this error:

error: 'const class std::unique_ptr' has no member named 'isClicked'|

I have a feeling I'm having a slight syntactical issue, and I'd be extremely grateful if someone was to explain it to me.

Thanks!

Was it helpful?

Solution

I think you mean:

dynamic_cast<SButton*>(SSceneManager::getGUIManager().getObject("testbutton").get())->isClicked()

You want to call isClicked on the result of the dynamic_cast, not the result of getObject.

OTHER TIPS

This line has several problems:

if (dynamic_cast<SButton*>(SSceneManager::getGUIManager().getObject("testbutton").isClicked()))

First SSceneManager::getGUIManager().getObject("testbutton") return a unique_ptr reference. And as the compiler said, unique_ptr does not hae an isclicked method. For that, you would need to use the -> operator which is overloaded to return the underlying pointer.

Second, even if it worked, you can not dynamic_cast a bool to a pointer.

You could do something like

if (dynamic_cast<SButton*>(SSceneManager::getGUIManager().getObject("testbutton").get())->isClicked) ...

Although you might want to separate it in 2 lines to make sure dynamic_cast does not give you a NULL pointer.

SBButton* button = dynamic_cast<SButton*>(SSceneManager::getGUIManager().getObject("testbutton").get());
if (button && button->isClicked()) ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top