This might be a silly question, but I’m having some trouble here. I thought I understood pointers but this is throwing me for a loop.
[ note: I’m using oF 0.8.4 on OSX ]
issue:
I have a Shirt class that stores 3 pointers to 3 vectors that are created by a separate Body class.
Within Shirt, there’s a function that draws the points to screen if they exist. Everything works fine unless the Body class fails to create any of the vectors. In that case, Shirt’s pointer to the vector should be NULL and it shouldn’t draw.
But instead I get a bad access exception in ofVec3f.h at line 258:
inline ofVec3f::ofVec3f( const ofVec2f& vec ):x(vec.x), y(vec.y), z(0) {}
Here’s my Shirt header:
class Shirt {
public:
Shirt();
void updateTorso(vector<ofVec2f>& torsoOutline);
void updateLArm(vector<ofVec2f>& lArmOutline);
void updateRArm(vector<ofVec2f>& rArmOutline);
void drawPoints();
vector<ofVec2f>* _torso;
vector<ofVec2f>* _lArm;
vector<ofVec2f>* _rArm;
};
and my class definition:
#include "Shirt.hpp"
Shirt::Shirt(){
_torso = NULL;
_lArm = NULL;
_rArm = NULL;
}
void Shirt::updateTorso(vector<ofVec2f>& torsoOutline){
_torso = &torsoOutline;
}
void Shirt::updateLArm(vector<ofVec2f>& lArmOutline){
_lArm = &lArmOutline;
}
void Shirt::updateRArm(vector<ofVec2f>& rArmOutline){
_rArm = &rArmOutline;
}
void Shirt::drawPoints(){
ofPushStyle();
// torso - blue
if (_torso){
ofSetColor(ofColor::blue);
for (int i=0; i<4; i++){
ofCircle((*_torso)[i], 3);
}
}
// left arm - green
if (_lArm){
ofSetColor(ofColor::green);
for (int i=0; i<4; i++){
ofCircle((*_lArm)[i], 3);
}
}
// right arm - red
if (_rArm){
ofSetColor(ofColor::red);
for (int i=0; i<4; i++){
ofCircle((*_rArm)[i], 3);
}
}
ofPopStyle();
}
Shouldn’t the if (_lArm) only run if _lArm is pointing to something? That’s where I get the exception (i.e. when I create a torso but no left arm).