Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Cant use Shape class in another class [SOLVED]  (Read 714 times)

0 Members and 1 Guest are viewing this topic.

flrinul

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Cant use Shape class in another class [SOLVED]
« on: March 04, 2023, 01:28:31 pm »
Hi, im trying to learn how to use files for classes in order to make the code more clean, and when i create a class called "Ball" and I try to inherit the "Shape" class i get an error saying that such class doesent exist

This is the Ball.cpp file:
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <ctime>
#include <cstdlib>

class Ball : public Shape {
public:
    float xSpeed, ySpeed;

    Ball() {
        CircleShape shape(15);
    }

    virtual std::size_t getPointCount() const {
        return 30;
    }

    virtual sf::Vector2f getPoint(std::size_t index) const {
        static const float pi = 3.141592654f;

        float angle = index * 2 * pi / getPointCount() - pi / 2;
        float x = std::cos(angle) * m_radius.x;
        float y = std::sin(angle) * m_radius.y;

        return sf::Vector2f(m_radius.x + x, m_radius.y + y);
    }
    void moveBall() {

    }
private:
    Vector2f m_radius;

};

This is the main.cpp file:
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Ball.h"

using namespace sf;
using namespace std;



int main() {

    RenderWindow window(VideoMode(1000, 600), "brick game");
    Clock clock;

    Ball ball = Ball();



    RectangleShape player(Vector2f(200, 20));
    player.setPosition(0, 580);

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

        float deltaTime = clock.restart().asSeconds();

        Vector2i mousePos = Mouse::getPosition(window);
        if (mousePos.x > 100 && mousePos.x < 900)
            player.setPosition(Vector2f(mousePos.x - 100, 580));

        window.clear();

        window.draw(player);

        window.display();
    }
}

When I declare the class in main.cpp it works fine, but here it doesent.
The project uses static linking for  the SFML files if it helps with anything
« Last Edit: March 04, 2023, 02:20:12 pm by flrinul »

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: Cant use Shape class in another class
« Reply #1 on: March 04, 2023, 02:03:19 pm »
Shape is part of the sf namespace, so you need to do:
class Ball : public sf::Shape {

It worked in main.cpp because of the using namespace sf; near the top.
"using namespace" only applies to the cpp file it is in, not others in the project.

flrinul

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Cant use Shape class in another class
« Reply #2 on: March 04, 2023, 02:21:30 pm »
Shape is part of the sf namespace, so you need to do:
class Ball : public sf::Shape {

It worked in main.cpp because of the using namespace sf; near the top.
"using namespace" only applies to the cpp file it is in, not others in the project.

Oh wow thx a lot. Cant belive I missed it