problem adding objects to std::vector

Hi,

I'm trying to add an object to a std::vector. I searched the web and this site and it looks like it shouldn't be a problem. My code runs fine right up until I get to the pathSteps.push_back( ... ) calls. The program is able to create the Step, it just can't add it to the vector.

Any help is very appreciated. Been plagued by this problem all day! Can provide more code or clarifications upon request.

uiberto


Here's a tidy version of my code:

// CLASS DEFINITIONS


class Step {

public:

Step(double t, int s);
Step(const Step& p); // copy constructor
Step& operator=(const Step& p){ // overloaded assignment operator
time = p.time;
state = p.state;
return *this;}

private:

double time;
int state;

};


class Path {

public:

Path();
std::vector<Step> getPath() { return pathSteps; }
void pushStep(double t, int s); // push_back Step to pathSteps vector

private:

std::vector<Step> pathSteps;

};



// METHOD DEFINITIONS

Path::Path() {

pathSteps.clear();
}

void Path::pushStep(double t, int s) {

pathSteps.push_back(Step(t, s)); // PROGRAM CRASHES
}

void Path::pushStep(Step p) {

pathSteps.push_back(p); // PROGRAM CRASHES
}

Step::Step(const Step& p) {

time = p.time;
state = p.state;
}

Step::Step(double t, int s) {

time = t;
state = s;
}
First of all do not define the copy constructor and assignment operator for the Step class. It isn't necessary. Second read the link that I posted and repost your example in a more readable format. Also provide a short main function that demonstrates the problem so that I can copy and paste it into a project, compile it, and run it.
Smells a lot like OP is mallocing a Path instead of using new.
Howdy,

I botched this one.

I went ahead and rewrote it as a simple main & class setup and it worked fine.

I was using a more complicated setup with std::vector<Path> rather than a single Path. The vector was having difficulties handling its Path elements. Once I cleaned that up, the Path.push_back(Step) problem disappeared.

Thanks for the quick response.

uiberto
Topic archived. No new replies allowed.