Vector of pointers , undefined class list , beg for help?

I try to use the vector of pointer to replace the vector of objects .
but the carlist become undefined …
why ? how could i do ?

void ofApp::setup(){

pSensor.setup(-1);
thisCar.setup();
numCars =800;
for(int i =0;i<numCars;i++){
	
	Car c ;
	c.setup();
	
	carList.push_back(new Car());
}

}

void ofApp::update(){

y=ofMap(pSensor.BPM,70,120,200,40);
numCars =y;

//update the list of cars
for (int i =0;i<numCars;i++){
	
	carList[i].update();     // carList become undefined 
}

}

carList is undefined
here is the app.h:

int numCars;
vector<Car*> carList;
  1. It doesn’t look like carList is undefined, but the way you’re calling update() is triggering errors. You should switch that to carList[i]->update(); The arrow operator (->) lets you use the thing you’re pointing to, instead of the pointer itself.

  2. Your for loop in setup() looks like it’s not doing what you think it’s doing? What you’re doing there is creating a new Car called c, and then just throwing it away. If you want to create a vector of 800 objects, and call setup() on all of them, try this instead:

for(int i =0;i<numCars;i++){
    carList.push_back(new Car());
    carList.back()->setup();
}

The function back() on the vector will give you the last object, which in this case will be the Car * that just got created.

Thx ! but there is also a problem in draw .
void ofApp::draw(){

if(pSensor.BPM>0){

ofSetColor(0,0,0);
ofDrawBitmapString(ofToString(pSensor.BPM), 10, 200);
//draw the single car
//thisCar.draw();


//draw the list of cars
for (int i =0;i<numCars;i++){
	carList[i]->draw;
}

}

}

Error 3 error C3867: ‘Car::draw’: function call missing argument list; use ‘&Car::draw’ to create a pointer to member
what should i do ?!

You’re missing the () on your draw call :wink:

carList[i]->draw;

to

carList[i]->draw();

haha , cheer ,
Thanks ,admsyn !! It works !

Admsyn! I’m sorry !
another problem !

void Car::update(){

if(pSensor.BPM>0){
	width=ofMap(pSensor.BPM,60,180,100,10);}
if(pSensor.BPM=0){
	width=0;}




float timeY = ofGetElapsedTimef() *0.05;
 amplitude = thisRadius + ofRandom(-0.5, 0.5);
 

yPos = ofGetWindowHeight()/2 + amplitude * sin(timeY)+x;

float timeX = ofGetElapsedTimef() * 0.05;

float homeX = ofGetWindowWidth()/2;

xPos = ofGetWindowWidth()/2 + amplitude* cos(timeX);

}

void Car::draw(){

ofSetColor(y,y,y);

ofCircle(xPos, yPos, width);}

I want to use the bpm sent by pulsesensor to control the width , but it doesn’t work !
However , I can use the bpm to control the numcars in app…
what should i do ?

ps: the y =o
so ofSetColor(y,y,y); should be ofSetColor(0,0,0);