2017-04-24 108 views
-5

整個項目很長,所以我剛纔包含了相關的行。沒有操作符「<=」匹配這些操作數

#include "stdafx.h" 
#include <iostream> 
#include <string> 

string list[50][50]; 
string a, b; 

for (int i = 0; i<50; i++) { 
    cout << "Insert a" << endl; 
    cin >> a; 
    cout << "Insert b" << endl; 
    cin >> b; 
    list[i][i] = { 
       {a}, 
       {b} 
    }; 
} 

賦值運算符「=」是唯一有錯誤的。錯誤是:「無運算符」=「匹配這些操作數,操作數類型是:std :: string = {...}」

我找不出這裏的問題。如果我運行程序,它會正確地分配「a」,但不是「b」。

+2

你不能插入兩個字符串到同一個地方。此外,使用現在的索引,它只會填充數組的對角線。 – InternetAussie

+4

你的標題是'<=',但你的文字是'='。 – aschepler

+1

你認爲那條線有什麼作用?左邊的東西只有一個字符串,但右邊的是兩個字符串。你如何設定一個等於另一個? –

回答

1

在我看來,你使用二維數組感到困惑。我想你想要的是這樣的:

for (int i = 0; i<50; i++) { 
    cout << "Insert a" << endl; 
    cin >> a; 
    cout << "Insert b" << endl; 
    cin >> b; 
    list[i][0] = a; 
    list[i][1] = b; 
} 

你的代碼進一步尋找它可能是更容易適應:

std::string list[50][50]; 
std::string a , b; 
int innerArraySize = 2; 
for (int i = 0; i<50; i++) 
{ 
    for (int j = 0; j < innerArraySize; j++) 
    { 
     cout << "Insert " << (char)(j + 'a'); 
     cin >> list[i][j]; 
    } 

} 
+0

這不會編譯。原始數組不可分配。 – aschepler

0

你想要的2 50個數組的數組? 然後你應該這樣做:

string list[50][2]; 
// . . . 
list[i][0] = a; 
list[i][1] = b; 
+0

這不會編譯。原始數組不可分配。 – aschepler

+0

已修復並已編輯。 – InternetAussie

+0

我認爲他/她想要有50x50矩陣。 – sergiol

相關問題