무작위로 배치 된 사각형의 벡터를 만들고이를 화면에 그리려면 벡터에 대한 참조를 전달하여 작업 할 수 없었습니다. (C++ SFML 모양의 벡터 만들기 및 함수에서 그릴 드로잉
consumable.h
#ifndef CONSUMABLE_H
#define CONSUMABLE_H
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
class consumable
{
public:
consumable();
virtual ~consumable();
vector<RectangleShape> fCreateConsumable(vector<RectangleShape>& vConsumable);
void fDrawTarget(float x, float y, RenderWindow &thatWindow);
protected:
private:
vector<RectangleShape> vConsumable;
RectangleShape _consumable;
};
consumable.cpp
#include "consumable.h"
consumable::consumable()
{
//ctor
}
consumable::~consumable()
{
//dtor
}
void consumable::fCreateConsumable(){
int consumableX{0}, consumableY{0};
for(int i=0;i<4;i++){
consumableX = (rand() % 31) + 1;
consumableY = (rand() % 22) + 1;
_consumable.setPosition((consumableX * 25), (consumableY * 25));
_consumable.setSize(sf::Vector2f(25.0f,25.0f));
_consumable.setFillColor(sf::Color::Magenta);
vConsumable.push_back(_consumable);
}
}
void consumable::fDrawTarget(float x, float y, RenderWindow &thatWindow){
void fCreateConsumable();
for(int i{0};i< vConsumable.size();i++){
thatWindow.draw(vConsumable[i]);
}
}
MAIN.CPP
012,377내가 클래스 consumable
보면
consumable.h를 포함 할 때 main.cpp 사본을 포함 시켰습니다. 이 문제를 해결하려면 질문을 편집하십시오. –
필자는'consumableX'와'consumableY'를'const int'로 루프 안에서 선언 할 것입니다. 마찬가지로,'_consumable'을'fCreateConsumable' (아마도 루프 내에서) 내에 로컬로 만들 것입니다. 'fDrawTarget'에서 루프를'for (const auto & item : vConsumable)) {thatWindow.draw (item);}' –
으로 작성했습니다. 마지막으로 "작동시키지 못했습니다"라는 식으로 말입니다. 컴파일러 오류 메시지 (정확하게 표시하십시오)? 예기치 않은 결과가 나왔습니까? (무엇?) 충돌? (어디?) –