'int rand()' cannot appear in a constant-expression

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 ^^"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <stdlib.h>
using namespace std;
const int window_width = 400;
const int 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
    virtual double 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;
    
}
You can't initialize speed with a function in the global scope like this (even though it is inside of a class). Put this in your constructor:
1
2
3
4
5
6
    Confetti (double a){
        width = a;
        xpos = rand() % window_width;
        ypos = rand() % window_height;
        speed = rand() % 4;
    }
The problem is still there, the error dissapeared. but after compiling i tried to run and had the following coming up:

http://i.imgur.com/1pFp1FL.jpg
This is now a completely different problem that probably has to do with going past the bounds of an array. Hold on...
Oh, this was an easy one!

Change line 79 from:
for (int i=0; i<=20; i++) {
to
for (int i=0; i<20; i++) {
It's another problem due to line 79. Replace <= with < and it should run without the crash
Wow ^^ works fine now, thanks for the fast help! :)
Topic archived. No new replies allowed.