【问题标题】:SFML Vector for Class Initializing用于类初始化的 SFML 向量
【发布时间】:2018-08-21 18:03:14
【问题描述】:

我想用 SF 向量初始化我的障碍类。 表示类看起来像这样:

Obstacle(Vector2f ObstaclePosition)

我的问题是,源文件应该是什么样的?我试过这样的事情:

Obstacle Obst1(300, 200)

但这显然没有用。

有人有想法吗?谢谢。

【问题讨论】:

  • 不应该是Obstacle Obst1(Vector2f(300.0f, 200.0f))吗?

标签: c++ vector sfml


【解决方案1】:

查看sf::Vector2<T>类的documentation,有三个可用的构造函数:

Vector2(); // empty

Vector2(T X, T Y); // two parameters

template <typename U>
explicit Vector2(const Vector2<U>& vector); // from another Vector object

所以你可以使用第二个构造函数,像这样:

Obstacle Obst1(Vector2f(300, 200));

const Vector2f v(300, 200);
Obstacle Obst1(v);

编译器应该能够接受initializer_list,所以你也可以这样做:

Obstacle Obst1({300, 200});

【讨论】:

    【解决方案2】:

    试试这个:

    Obstacle Obst1(Vector2f(300, 200))
    

    我不确定,但这也可能有效:

    Obstacle Obst1({300, 200})
    

    您的行和那些行之间的区别在于,在您的行中您尝试使用Obstacle(float, float) 构造函数(可能不存在),而在这些行中我使用的是Obstacle(Vector2f) 构造函数。

    【讨论】:

    • 哦,比我想象的要容易,非常感谢!两种方式都有效,是的,感谢您的解释:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    • 1970-01-01
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多