Hello,
Ive been looking for a solution for this for a couple hours now, but couldnt manage to find out whats wrong. Actually the code itself "works" somehow, i can compile it and even run it, but on windows it crashes my cmd. and on MAC it gives out a warning.
Compiling works, running causes problems, so i went on different sites like ideone or codepad to check for errors and codepad gave me this errors:
Line 11: error: 'int rand()' cannot appear in a constant-expression
compilation terminated due to -Wfatal-errors.
Since im a beginner with c++ i am kind of confused how to solve this, if someone could please help me out with a hint ^^"
#include <iostream>
#include <stdlib.h>
usingnamespace std;
constint window_width = 400;
constint window_height = 300;
//superclass definition
class Confetti {
public:
double width;
int xpos, ypos;
int speed = rand() % 4; // declare speed
Confetti (double a){
width = a;
xpos = rand() % window_width;
ypos = rand() % window_height;
}
// the keyword virtual makes it a method definition that has to
// be overwritten later
virtualdouble area() {
return 0.0;
}
// if statement to give the command to change the position when it hits the border
int movex () {
if (xpos<window_width){
xpos = xpos+speed;
} else {
xpos = xpos-speed;
}
return (xpos);
}
// if statement to give the command to change the position when it hits the border
int movey () {
if (ypos<window_height){
ypos = ypos+speed;
} else {
ypos = ypos-speed;
}
return (ypos);
}
};
//subclass definition of Confetti
class CCircle: public Confetti {
public:
CCircle (double a): Confetti(a) {};
double area (){
return (width * 3.14);
}
};
//subclass definition of Confetti
class CRectangle: public Confetti {
public:
double height;
CRectangle(double a, double b): Confetti(a){
height=b;
}
double area (){
return (width * height);
}
};
//subclass definition of Confetti
class CTriangle: public Confetti {
public:
double height;
CTriangle(double a, double b): Confetti(a){
height=b;
}
double area (){
return (width * height / 2);
}
};
int main () {
// array of pointers to three objects of Confetti
Confetti * conf [20];
// create objects from a different subclass
for (int i=0; i<20; i++){
conf[i] = new CCircle(rand()%3);
}
for (int i=0; i<=20; i++) {
int ii =i+1;
cout << "confetti " << ii << " position: (" << conf[i]->movex() << "/" << conf[i]->movey() << ")" << endl;
}
return 0;
}