problem with random values

Hi everyone,

I am a beginner in both OF and C++ … so please apologize my lack of knowledge.
I have a background in 3D and python scripting though, so not a complete noob either ^^

I have made a simple class : Ball.cpp -> a simple circle 3 properties x, y and uniqueVal

I am trying to “instanciate” this class 5 times and give each Ball a different start position.
I included my code below. I don’t have any errors at compilation. The problem is all 5 balls have the same position.
I guess I am not calling the setStartPos() method at the right place. I tried calling it at the same moment I call b[i].draw() doing b[i].setStartPos() , but then I have an error telling me Ball doesn’t have a member call setStartPos() . But it HAS a member setStartPos()

I don’t understand where the problem is.

Ball.cpp :

  
  
#include "ball.h"  
  
Ball::Ball(){  
  
    ofSeedRandom();  
    // Set the initial color  
    color.set( ofRandom(0,255), ofRandom(0,255), ofRandom(0,255));  
  
    // Initial x position of the ball  
    x = ofGetWindowWidth()/2;  
  
    // Initial y position of the ball  
    y = ofGetWindowHeight()/2;  
  
    setStartPos();  
  
  
}  
  
void Ball::setStartPos(){  
    uniqueVal = ofRandom(-100,100);  
    x = x + uniqueVal;  
    y = y + uniqueVal;  
}  
  
  
  
void Ball::draw(){  
    ofSetColor(color);  
    ofFill();  
    ofCircle( x, y, 30);  
}  
  
  

Ball.h :

  
  
#ifndef BALL_H_INCLUDED  
#define BALL_H_INCLUDED  
  
#include "ofMain.h"  
class Ball {  
public:  
    //Constructor  
    Ball();  
  
    //methods  
public:  
    void setStartPos();      
    void draw();  
  
    //properties  
    int uniqueVal;  
    int x;  
    int y;  
    ofColor color;  
};  
#endif // BALL_H_INCLUDED  
  
  
  

testApp.cpp :

  
  
  
#include "testApp.h"  
#include <iostream>  
  
using namespace std;  
void testApp::setup(){  
    // Smooth edges  
    ofEnableSmoothing();  
  
    // Fixed framerate  
    ofSetFrameRate(30);  
  
    const int num = 5;  
    b.assign(num, Ball());  
}  
  
void testApp::update(){  
  
}  
  
void testApp::draw(){  
    ofBackgroundGradient(ofColor::gray,ofColor(30,10,30), OF_GRADIENT_CIRCULAR);  
  
    for(unsigned int i=0;i<b.size();i++){  
        b[i].draw();  
    }  
    cout << b[0].x << endl;  
}  
  
  

testApp.h :

  
#ifndef TESTAPP_H_INCLUDED  
#define TESTAPP_H_INCLUDED  
  
#include "ofMain.h"  
#include "ball.h"  
  
class testApp : public ofBaseApp{  
public:  
    void setup();  
    void update();  
    void draw();  
    vector <Ball> b;  
};  
  
#endif // TESTAPP_H_INCLUDED  

this is a common problem with vectors, assign or resize won’t create 5 new instances but instead 1 instance and then 4 copies of the first one so the constructor will be called only one. The way to create 5 different instances is to use a for loop and put the instances in the vector using push_back.

thank you for your answer. I am going to try this ^^

it works fine thanks

I did a simple for loop as you said

  
  
    for(int j=0; j<5; j++){  
        b.push_back(Ball());  
    }  
  

But I still don’t understand why Ican write : b[n].draw() -> no errors
and when I write b[n].setStartPos() the compiler complains that setStartPos() is not a member of Ball().
But it is just as much as draw() , is it not ? or am I getting something wrong ?

no you should be able to call any method of the class, you probably just have a typo

then I just don’t get it … I called the method ‘aaa’ ( can’t miss that ), and I still get the error.
But it works of I call aaa(); inside the class contructor.

can you post the full code? at least the .h of Ball and the .cpp of testApp

here is my code :
ball.cpp :

  
  
#include "ball.h"  
  
Ball::Ball(){  
  
  
    // Initial x position of the ball  
    x = ofGetWindowWidth()/2;  
  
    // Initial y position of the ball  
    y = ofGetWindowHeight()/2;  
  
    aaa(ofRandom(0,100));  
  
  
}  
  
void Ball::aaa(int seed){  
  
    //ofSeedRandom(seed);  
  
    uniqueVal = ofRandom(-100,100);  
    cout<<seed<<endl;  
    x = x + seed;  
  
    y = y + seed;  
  
}  
  
  
  
void Ball::draw(){  
    ofSetColor(color);  
    ofFill();  
    ofCircle( x, y, 5);  
    //cout<<"hi !"<<endl;  
}  
  

ball.h :

  
  
#ifndef BALL_H_INCLUDED  
#define BALL_H_INCLUDED  
  
#include "ofMain.h"  
class Ball {  
public:  
    //Constructor  
    Ball();  
  
    //methods  
  
    void aaa(int seed);  
    void draw();  
  
    //properties  
    int uniqueVal;  
    int x;  
    int y;  
    ofColor color;  
};  
#endif // BALL_H_INCLUDED  
  

testApp.cpp :

  
#include "testApp.h"  
#include <iostream>  
  
using namespace std;  
void testApp::setup(){  
  
    for(int j=0; j<25; j++){  
        b.push_back(Ball());  
        //b[j].aaa(ofRandom(0,100)); // ----- this is where I try to call aaa() but it throws an error  
    }  
}  
  
  
  
void testApp::draw(){  
    ofBackgroundGradient(ofColor::gray,ofColor(30,10,30), OF_GRADIENT_CIRCULAR);  
  
    for(unsigned int i=0;i<b.size();i++){  
        b[i].draw();  
    }  
}  

testApp.h :

  
  
#ifndef TESTAPP_H_INCLUDED  
#define TESTAPP_H_INCLUDED  
  
#include "ofMain.h"  
#include "ball.h"  
using namespace std;  
class testApp : public ofBaseApp{  
public:  
    void setup();  
    void draw();  
    vector<Ball> b;  
};  
  
#endif // TESTAPP_H_INCLUDED  
  

that seems correct to me can you post the exact error you are getting?

I get this :

E:\OPEN_FRAMEWORK\of_v0.8.0_win_cb_release\apps\myApps\test_02\src\testApp.cpp||In member function ‘virtual void testApp::setup()’:expressionless:
E:\OPEN_FRAMEWORK\of_v0.8.0_win_cb_release\apps\myApps\test_02\src\testApp.cpp|9|error: ‘class Ball’ has no member named ‘aaa’|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 2 seconds) ===|

is it possible that you’ve created 2 different Ball classes and the one that codeblocks is compiling is not the one with the method? look in the tab of the file you are editing for the path and then look in the file system to see if there’s any other Ball classes. the syntax is correct so that’s the only thing that come to mind that can be happening

I checked my code::blocks project, didn’t find any duplicate files.
So I restarted from an empty project. This time I used the “project Generator”, and IT WORKS FINE ^^.
I can create and call any method I want. I feel much better. I was beginning to think OF was too complicated for me.
I don’t know if it was a bug or what, but I am glad it’s no longer there.

thank you very much for your support ARTURO ^^