2

我创建了一个类,其中包括向量。我想循环这些向量,因此在我的类构造函数中创建了一个指向这些向量的指针向量。它编译正常,但运行不正常。调试似乎表明 vector.size() 存在问题,尽管它出现在 cout 行上。我已经缩短了课程和代码以使其更易于阅读,但我仍然收到错误消息。

这是类定义:

#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

这是源文件:

#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;
}

我将非常感谢任何可以帮助我解决这个问题的线索。

4

2 回答 2

2

variable_spline3d.assign(temp_spline3d, temp_spline3d + 5);

assign()要复制五个 Spline3D*指针,但数组中只有四个指针,所以第 5 个指针是垃圾。

于 2012-12-12T21:26:13.810 回答
1

您没有检查大小spline->location.branches。很可能存在一个空的分支向量,而您正试图访问一个不存在的元素。

于 2012-12-12T21:18:46.000 回答