Reputation: 21
I have created a class including among other things vectors. I would like to loop these vectors and have therefore in my class constructor created a vector of pointers to these vectors. It compiles OK but it doesn't run properly. Debugging seems to show that there is a problem with the vector.size() although it appears on the cout line. I have shorten the class and the code to make it easier to read but I still get the error.
This is the class definition:
#ifndef DEFINITION_CLASSES
#define DEFINITION_CLASSES
#include <string>
#include <vector>
#include <stdarg.h>
using namespace std;
class objLoc {
public:
vector<string> branches;
void setBranches(int amount, ...);
};
void objLoc::setBranches(int amount, ...) {
char *value;
va_list points;
branches.clear();
va_start(points, amount);
for (int i = 0; i < amount; ++i) {
value = va_arg(points, char *);
branches.push_back(string(value));
}
va_end(points);
}
class Spline3d {
public:
vector<double> x, y, z;
objLoc location;
void setx(int amount, ...);
};
void Spline3d::setx(int amount, ...) {
double value;
va_list points;
x.clear();
va_start(points, amount);
for (int i = 0; i < amount; ++i) {
value = va_arg(points, double);
x.push_back(value);
}
va_end(points);
}
class SuspensionGeneralProperties {
public:
Spline3d left_x_vs_z, left_y_vs_z, right_x_vs_z, right_y_vs_z;
};
class Suspension {
public:
Suspension();
SuspensionGeneralProperties suspProp;
vector<Spline3d *> variable_spline3d;
};
Suspension::Suspension() {
Spline3d * temp_spline3d[] = { &suspProp.left_x_vs_z, &suspProp.left_y_vs_z,
&suspProp.right_x_vs_z, &suspProp.right_y_vs_z };
variable_spline3d.assign(temp_spline3d, temp_spline3d + 5);
}
#endif
And here is the source file:
#include <iostream>
#include <string>
#include <vector>
#include <stdarg.h>
#include "class_def.h"
using namespace std;
void loop(Suspension* susp) {
Spline3d* spline;
for (size_t i = 0; i < susp->variable_spline3d.size(); i++) {
spline = susp->variable_spline3d[i];
cout << "Branch: " << spline->location.branches[0] << endl; //This is where the error seems to be
}
}
int main() {
int i;
Suspension suspFront;
suspFront.suspProp.left_x_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
suspFront.suspProp.left_x_vs_z.location.setBranches(3, "Subsystem",
"SuspensionGeneralProperties", "Spline3d");
suspFront.suspProp.left_y_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
suspFront.suspProp.left_y_vs_z.location.setBranches(3, "Subsystem",
"SuspensionGeneralProperties", "Spline3d");
suspFront.suspProp.right_x_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
suspFront.suspProp.right_x_vs_z.location.setBranches(3, "Subsystem",
"SuspensionGeneralProperties", "Spline3d");
suspFront.suspProp.right_y_vs_z.setx(11, -100.0, -80.0, -60.0, -40.0, -20.0,
0.0, 20.0, 40.0, 60.0, 80.0, 100.0);
suspFront.suspProp.right_y_vs_z.location.setBranches(3, "Subsystem",
"SuspensionGeneralProperties", "Spline3d");
loop(&suspFront);
cin >> i;
return i;
}
I would be very grateful for any leads that can help me solve this.
Upvotes: 2
Views: 141
Reputation: 596277
variable_spline3d.assign(temp_spline3d, temp_spline3d + 5);
You are telling assign()
to copy five Spline3D*
pointers but there are only four pointers in your array, so the 5th pointer is garbage.
Upvotes: 2
Reputation: 56
You are not checking to see what the size of spline->location.branches
is. It's likely that there is an empty branches vector and you are attempting to access a nonexistent element.
Upvotes: 1