2012-12-12 68 views
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; 
} 

我將是任何線索,可以幫助我解決此非常感謝。

+0

哦,天啊,不是'stdarg.h'! – Pubby

+0

首選'矢量>'並讓矢量完成它的工作。 –

+1

哦,上帝,不是'使用命名空間std'! :-) –

回答

1

您不檢查spline->location.branches的尺寸是多少。很可能是有一個空的分支矢量,並且你試圖訪問一個不存在的元素。

2

variable_spline3d.assign(temp_spline3d,temp_spline3d + 5);

你告訴assign()複製 Spline3D*指針但只有數組中的指針,所以第5指針是垃圾。

+1

@Bill,重新考慮。 '[&foo,&foo + 1]'包含多少個元素? – Yakk

+2

@Bill因此在[temp_spline3d,temp_spline3d + 5)中有5個元素,並且Remy正確,因此存在問題。 – Yakk