2014-04-21 27 views
0

這是錯誤「沒有重載函數的實例...」。當我嘗試通過一個以上的論證時,我會得到它。當我從文件中除去一個時,它工作正常。使用std :: vector和class對象時出錯

這裏是我得到錯誤的ObjectHandler.cpp。

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

這裏是該課程的hpp。

#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 

這是cpp文件。

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

我不明白爲什麼會發生這種情況......我試圖通過搜索谷歌沒有運氣得到答案。我非常感謝任何幫助! :)

回答

1

你需要構建一個實際的平臺,你只是試圖把一堆Vector2fColor對象的時刻到你的platforms_載體中。

例如

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

下面的方式也應該工作,編譯器會從初始化值列表,並在端部推斷的類型調用相同的構造,如上面的例子。

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

但是,爲了避免不必要的複製,您應該將它置於矢量上而不是推送它。

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

這樣做是構建就地對象上的向量,看到cppreference有關emplace_back更多信息。

+0

我會研究它。 :) – user1209460

1

我認爲這是

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

而不是

platforms_.push_back(sf::Vector2f(0, 680), sf::Vector2f(40, 2000) , sf::Color(100, 255, 40)); 
相關問題