2012-12-12 2 views
2

다른 것들을 포함하여 클래스를 만들었습니다. 나는이 벡터들을 반복하고 싶고 그러므로 나의 클래스 생성자에서이 벡터들에 대한 포인터의 벡터를 만들었습니다. 그것은 OK를 컴파일하지만 제대로 실행되지 않습니다. 디버깅은 cout 행에 표시 되더라도 vector.size()에 문제가 있음을 보여줍니다. 나는 클래스와 코드를 읽기 쉽도록 단축했다. 그러나 나는 여전히 오류를 얻는다. 여기 포인터의 벡터를 사용하여 다른 벡터를 반복하기

#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

'vector >'을 선호하고 벡터가 그 일을하도록하십시오. –

+1

오 하나님,'네임 스페이스 표준을 사용하지! :-) –

답변

1

spline->location.branches의 크기를 확인하지 않았습니다. 빈 브랜치 벡터가 있고 존재하지 않는 요소에 액세스하려고 시도하는 것 같습니다.

2

variable_spline3d.assign (temp_spline3d, temp_spline3d + 5);

당신은 다섯Spline3D* 포인터를 복사 assign()을 말하고있다하지만 5 포인터가 쓰레기가 귀하의 배열 만 포인터가있다.

+1

@Bill, 다시 검토하십시오. '[& foo, & foo + 1)'에는 몇 개의 요소가 들어 있습니까? – Yakk

+2

@Bill 따라서 [temp_spline3d, temp_spline3d + 5]에는 5 개의 요소가 있으며 Remy는 문제가 있다는 점에서 맞습니다. – Yakk

관련 문제