Question

This is the error "no instance of overloaded function...". I get it when I try to pass more than one argument. It works fine when I remove all but one from the files.

Here is the ObjectHandler.cpp where I get the error.

    #include <SFML\Graphics.hpp>

    #include <memory>

    #include "ObjectHandler.hpp"
    #include "Platform.hpp"
    #include "Game.hpp"

    ObjectHandler::ObjectHandler()
    {
    platforms_.push_back(sf::Vector2f(0, 680), sf::Vector2f(40, 2000)
, sf::Color(100, 255, 40)); //This is the line where I get the error.
}

void ObjectHandler::render(sf::RenderWindow& window)
{
    for (auto platform : platforms_)
        platform.render(window);
}

Here is the hpp for the class.

#ifndef PLATFORM_HPP
#define PLATFORM_HPP

#include <SFML\Graphics.hpp>

class Platform
{
public:
    Platform(sf::Vector2f position, sf::Vector2f size, sf::Color color);
    void render(sf::RenderWindow& window);

    sf::Vector2f getPosition() const;
    sf::FloatRect getBounds() const;
private:
    sf::RectangleShape platform_;
    sf::Vector2f position_;
};

#endif

Here is the cpp file.

#include <SFML\Graphics.hpp>

#include "Platform.hpp"

Platform::Platform(sf::Vector2f position, sf::Vector2f size, sf::Color color)
    : position_(position)
{
    platform_.setPosition(position);
    platform_.setFillColor(color);
    platform_.setSize(size);
}

sf::FloatRect Platform::getBounds() const
{
    return platform_.getGlobalBounds();
}

sf::Vector2f Platform::getPosition() const
{
    return position_;
}

void Platform::render(sf::RenderWindow& window)
{
    window.draw(platform_);
}

I don't understand why this is happening... I have tried to get an answer by searching google with no luck. I really appreciate any help! :)

Was it helpful?

Solution

You need to construct an actual platform, at the moment you are just trying to push a bunch of Vector2f and Color objects into your platforms_ vector.

For example

platforms_.push_back(Platform(sf::Vector2f(0, 680),
    sf::Vector2f(40, 2000), sf::Color(100, 255, 40)));

The following should also work, the compiler will deduce the type from an initializer list and in the end call the same constructor as in the above example.

platforms_.push_back({sf::Vector2f(0, 680),
    sf::Vector2f(40, 2000), sf::Color(100, 255, 40)});

However, in order to avoid the unnecessary copy here you should just emplace it on the vector rather than push it.

platforms_.emplace_back(sf::Vector2f(0, 680),
    sf::Vector2f(40, 2000) , sf::Color(100, 255, 40));

What this does is construct the object in-place on the vector, see cppreference for more information about emplace_back.

OTHER TIPS

I think it is

platforms_.push_back(Platform(sf::Vector2f(0, 680), sf::Vector2f(40, 2000) , sf::Color(100, 255, 40)));

rather than

platforms_.push_back(sf::Vector2f(0, 680), sf::Vector2f(40, 2000) , sf::Color(100, 255, 40));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top