0
我正在寫一個簡單的動畫來繪製一個球從牆壁反彈。這裏是類:使用框架,錯誤的構造函數C++崩潰
class Ball{
private:
int x,y; //position
int vx,vy; //velocity
int rad=20; //radius
ofColor color; //color
public:
Ball(int a, int b, int c, int d,ofColor e);
Ball();
void draw();
void move();
};
當我構建球5個參數一切正常,但是當我使用了一個不帶參數的崩潰:
Ball::Ball(){
x=int(ofRandomWidth());
y=int(ofRandomHeight());
vx=int(ofRandom(-10,10));
vy=int(ofRandom(-10,10));
int r=int(ofRandom(0,255));
int g=int(ofRandom(0,255));
int b=int(ofRandom(0,255));
ofColor a(r,g,b);
color=a;
}
有什麼可以不對的構造?
ofRandom:
float ofRandom(float max) {
return max * rand()/(RAND_MAX + 1.0f);
}
//--------------------------------------------------
float ofRandom(float x, float y) {
float high = 0;
float low = 0;
float randNum = 0;
// if there is no range, return the value
if (x == y) return x; // float == ?, wise? epsilon?
high = MAX(x,y);
low = MIN(x,y);
randNum = low + ((high-low) * rand()/(RAND_MAX + 1.0));
return randNum;
}
'ofRandom'函數是否返回浮點值?在這種情況下,您應該使用'static_cast'而不是'int'構造函數來使它們成爲'int'。 – noobProgrammer
你可以將代碼發佈到ofRandom等嗎? – SamFisher83
爲什麼在作業需要爲您做好這些工作時,您會進行明確的轉換? – tadman