使用指针向量来循环其他向量

时间:2012-12-12 21:12:46

标签: c++ pointers vector

我创建了一个类,其中包括矢量。我想循环这些向量,因此在我的类构造函数中创建了一个指向这些向量的指针向量。它编译好,但它运行不正常。调试似乎表明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;
}

我会非常感谢能够帮助我解决这个问题的任何线索。

2 个答案:

答案 0 :(得分:2)

  

variable_spline3d.assign(temp_spline3d,temp_spline3d + 5);

您告诉assign()复制五个 Spline3D*指针,但数组中只有四个指针,因此第五个指针是垃圾

答案 1 :(得分:1)

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