2011-05-07 8 views
0

이 함수는 텍스트 파일을 읽고 배열에 문자열을 보낸 다음 해당 배열과 요소 수를 생성자에게 보냅니다. 이제 내 생성자는 동적 인 2D 배열을 생성합니다 (희망). 받은 2 차원 어레이의 행과 열에 배열 값을 할당하고 싶습니다.하나의 배열 내용을 다른 2 차원 배열 rows & colums로 만듭니다.

heres my constructor.

Graph::Graph(string cities[], int n) 
{ 
    this->nrOfCities=n; 
    this->x=n; 
    this->y=n; 
    this->graph=new string *[x]; 
    for (int i = 0; i < x; i++) 
     this->graph[i] =new string[y]; 
    for(int i=0;i<this->x;i++) 
     for(int j=0;j<this->x;j++) 
      this->graph[j]=NULL; 
    for(int i=0;i<=this->x;i++)//I know this last part doesn't work. 
     for(int j=0;j<this->x;j++) 
      this->graph[0][j+1]=cities[j]; 
} 

도움의 모든 종류의

에 감사드립니다.

+0

무엇을하기위한 두 번째 외부입니까? 내가 너를 사용하지 않는다면? 또한 세 번째 외부 용. – atoMerz

답변

1

동적 2darray를 만들려면 s.th를 시도해야합니다. 이렇게 :

type** arr2d; 
arr2d = new type*[rows]; 
for(int i=0; i<rows; ++i) 
    arr2d[i] = new type[cols]; 
+0

2darr은 숫자로 시작하므로 C++의 변수에 유효한 이름이 아닙니다. – jonsca

+0

이런 뜻인가요? 'this-> graph = new string [x]; for (int i = 0; i graph [i] = new string [y]; ' 이 작업은 저에게 효과적이지 않습니다. 할당 연산자에 대해 불평합니다. – ogward

+0

@ogward 정확하게 무엇을 말합니까? – atoMerz