2011-05-07 74 views
0

我有這個函數讀取一個文本文件並將字符串發送到一個數組,然後從該函數發送該數組和元素數到我的構造函數。現在我的構造函數創建一個動態二維數組(我希望)。我想我的二維數組的行和列從接收的數組中分配值。使一個數組的內容變成另一個二維數組rows和colums

繼承人我的構造函數。

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 2011-05-07 17:41:13

回答

1

爲了製作一個動態的2dray,你必須嘗試s.th.像這樣:

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

2darr不是C++變量的有效名稱,因爲它以數字開頭。 – jonsca 2011-05-07 18:24:22

+0

你的意思是這樣嗎? 'this-> graph = new string [x]; for(int i = 0; i graph [i] = new string [y];' 這不適合我,抱怨賦值運算符。 – ogward 2011-05-07 19:10:44

+0

@ogward這究竟是什麼意思? – atoMerz 2011-05-08 08:10:43

相關問題