繁体   English   中英

使用默认构造函数的sf :: Shape

[英]Using sf::Shape with a default constructor

我想创建一个以sf :: Shape作为成员变量的类,但是由于某些原因,我无法在默认构造函数中(仅在main中)设置其参数。

不知道为什么,错误提示“表达式必须具有类类型”。 感谢任何可以帮助您的人。

#include "stdafx.h"
#include <SFML/Graphics.hpp>


class SFshape : public sf::Shape
{

public:
    SFshape()
    {
        shape.setSize(sf::Vector2f(100, 100));


    }
private:
    sf::RectangleShape shape();

};

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML");


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.display();
    }


    return 0;
}

这个sf::RectangleShape shape(); 看起来像一个函数,而不是一个对象。 因此,看起来您正在尝试声明一个成员函数,而不是一个变量。 因此,它说它不是类类型

此时,您无论如何都不应调用任何构造函数。 您只需要声明一个变量sf::RectangleShape shape; 请注意,这样的语法sf::RectangleShape shape()不会调用默认构造函数... sf::RectangleShape shape会调用默认构造函数。

每个成员都有一个始终调用的默认构造函数,除非将其放在初始化程序列表中,但是您可以明确地进行操作:

class SFshape : public sf::Shape
{
  public:
    SFshape() : shape() //invoke shape's default constructor explicitly
    {
        shape.setSize(sf::Vector2f(100, 100));
    }
  private:
    sf::RectangleShape shape; // declare a member variable
};

我认为您打算这样做:

sf::RectangleShape shape;
// sf::RectangleShape shape(); <--- instead of this

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM