Charsmud
Charsmud

Reputation: 183

missing type specifier - int assumed. Note: C++ does not support default-int on function in .h file

I am following tutorials outlined in "SFML Game Development" by Packt Publishing. In chapter 4, I am having many errors, most notably " missing type specifier - int assumed. Note: C++ does not support default-int". Here is my header and source file that this is referring to:

#pragma once

#include <assert.h>
#include <algorithm>
#include <vector>
#include <SFML\Graphics.hpp>
#include "Category.h"
#include "Command.h"

class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable
{
public:
    typedef std::unique_ptr<SceneNode> Ptr;

public:
    SceneNode();
    void attachChild(Ptr);
    Ptr detachChild(const SceneNode&);
    void update(sf::Time);
    sf::Transform getWorldTransform() const;
    sf::Vector2f getWorldPosition() const;
    unsigned int getCategory() const;
    void onCommand(const Command&, sf::Time);

private:
    virtual void draw(sf::RenderTarget&, sf::RenderStates) const;
    virtual void drawCurrent(sf::RenderTarget&, sf::RenderStates) const;
    virtual void updateCurrent(sf::Time);
    void updateChildren(sf::Time);

private:
    std::vector<Ptr> mChildren;
    SceneNode* mParent;
};

#include "SceneNode.h"


SceneNode::SceneNode() : mChildren(), mParent(nullptr)
{

}

void SceneNode::attachChild(Ptr child)
{
    child->mParent = this;
    mChildren.push_back(std::move(child));
}
SceneNode::Ptr SceneNode::detachChild(const SceneNode& node)
{
    auto found = std::find_if(mChildren.begin(), mChildren.end(), [&] (Ptr& p) -> bool
    {
        return p.get() == &node;
    });
    assert(found != mChildren.end());

    Ptr result = std::move(*found);
    result->mParent = nullptr;
    mChildren.erase(found);
    return result;
}

void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    states.transform *= getTransform();
    drawCurrent(target, states);
    for(auto itr = mChildren.begin(); itr != mChildren.end(); ++itr)
    {
        (*itr)->draw(target, states);
    }
}

void SceneNode::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
{
}

void SceneNode::updateCurrent(sf::Time dt)
{

}
void SceneNode::update(sf::Time dt)
{
    updateCurrent(dt);
    updateChildren(dt);
}
void SceneNode::updateChildren(sf::Time dt)
{
    for(auto itr = mChildren.begin(); itr != mChildren.end(); ++itr)
    {
        (*itr)->update(dt);
    }
}

sf::Transform SceneNode::getWorldTransform() const
{
    sf::Transform transform = sf::Transform::Identity;
    for(const SceneNode* node = this; node != nullptr; node = node->mParent)
    {
        transform = node->getTransform() * transform;
    }
    return transform;
}
sf::Vector2f SceneNode::getWorldPosition() const
{
    return getWorldTransform() * sf::Vector2f();
}

unsigned int SceneNode::getCategory() const
{
    return Category::Scene;
}

void SceneNode::onCommand(const Command& command, sf::Time dt)
{
    if(command.category & getCategory())
    {
        command.action(*this, dt);
        for(auto itr = mChildren.begin(); itr != mChildren.end(); ++itr)
        {
            (*itr)->onCommand(command, dt);
        }
    }
}

The error is specifically on line 23:

void onCommand(const Command&, sf::Time);

Any ideas?

Upvotes: 0

Views: 15984

Answers (2)

godel9
godel9

Reputation: 7390

I'm guessing the file "Command.h" also includes this file... If that's the case, #pragma once doesn't do exactly what you'd expect, and you end up not having declared the class Command by the time you try to declare the onCommand member function.

Just add forward class declarations before your SceneNode class declaration, and you should be fine:

class Command;

class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable
{
    // etc.
};

You're seeing an error because the compiler hasn't seen the type Command yet, tries to fall back on int, and then realizes that C++ doesn't allow that anymore. (hat tip:@Lightness Races in Orbit)

See my answer to this question for a more detailed explanation. (Just for reference, the #import directive in Objective-C acts like #include plus #pragma once, so the logic is the same.)

Upvotes: 4

Mario
Mario

Reputation: 36497

This specific compiler error usually means that you've somehow screwed up a variable declaration, typically by using an unknown type or by malforming the line some other way.

Since sf::Time will be defined by the other SFML headers (in this case SFML/Graphics.hpp), this can't be the problem. Due to this, Command is most likely the culprit. However, to determine the exact reason without guessing, you'd have to post the contents of your Command.h file as well.

Upvotes: 0

Related Questions