关于sfml:缺少类型说明符-假定为int。 注意:C ++不支持.h文件中的default-int on函数

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

我正在关注Packt Publishing在" SFML游戏开发"中概述的教程。 在第4章中,我遇到了很多错误,最明显的是"缺少类型说明符-假定为int。注意:C ++不支持default-int"。 这是我引用的头文件和源文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#pragma once

#include
#include
#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);
        }
    }
}

该错误专门在第23行上:

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

有任何想法吗?


我猜文件" Command.h"也包含此文件...如果是这种情况,#pragma once不能完全满足您的期望,并且最终您没有声明类为Command您尝试声明onCommand成员函数的时间。

只需在SceneNode类声明之前添加前向类声明,就可以了:

1
2
3
4
5
6
class Command;

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

您会看到一个错误,因为编译器还没有看到类型Command,试图回退到int类型,然后意识到C ++不再允许该类型。 (帽子提示:@Lightness Races in Orbit)

请参阅我对这个问题的回答,以获得更详细的解释。 (仅供参考,Objective-C中的#import指令的作用类似于#include#pragma once,因此逻辑相同。)


这个特定的编译器错误通常意味着您已经以某种方式搞砸了变量声明,通常是通过使用未知类型或以其他方式使行格式错误。

由于sf::Time将由其他SFML标头(在本例中为SFML/Graphics.hpp)定义,所以这不是问题。因此,Command最有可能是罪魁祸首。但是,要确定确切的原因而不进行猜测,您还必须发布Command.h文件的内容。