2017-06-04 360 views
-2
#include <iostream> 

using namespace std; 

int** add(int** first, int** second); 
int addAllElems(int** matrix); 
int addAllElems(int** matrix); 
void display(int** matrix); 

int main() 
{ 
    int first** = {{5,7,4}, {3,9,2}, {7,3,6}}; 
    int second** = {{3,7,2}, {6,2,6}, {3,5,8}}; 

    cout << "The first matrix is:" << endl; 
    display(first); 
    cout << endl; 

    cout << "The second matrix is:" << endl; 
    display(second); 
    cout << endl; 

    cout << "The sum of the two matrices is:" << endl; 
    display(add(first, second)); 
    cout << endl; 

    cout << "The sum of all elements of the first matrix is: " 
     << addAllElems(first) << endl; 
    cout << endl; 

    return 0; 
} 

int** add(int** first, int** second) { 
    int[][] sum = new int[3][3]; 
    for (int i = 0; i <= 2; i++) { 
     for (int j = 0; j <= 2; j++) { 
      sum[i][j] = first[i][j] + second[i][j]; 
     } 
    } 
    return sum; 
} 

int addAllElems(int** matrix) { 
    int sumOfElems = 0; 
    for (int i = 0; i <= 2; i++) { 
     for (int j = 0; j <= 2; j++) { 
      sumOfElems += matrix[i][j]; 
     } 
    } 
    return sumOfElems; 
} 

void display(int** matrix) { 
    cout << "[" << endl; 
    for (int i = 0; i <= 2; i++) { 
     for (int j = 0; j <= 2; j++) { 
      cout << matrix[i][j] << ", "; 
     } 
     cout << endl; 
    } 
    cout << "]" << endl; 
} 

誰能解決這個問題的代碼?錯誤:預期初始化之前「*」標記

編譯當我得到這個錯誤:

In function 'int main()': 
12:14: error: expected initializer before '*' token 
63:1: error: expected '}' at end of input 

我認爲這個問題是從我的數組聲明。 我認爲這個問題來自我的數組聲明。 我認爲這個問題來自我的數組聲明。 我認爲這個問題來自我的數組聲明。

+2

指針的指針不是二維數組。 –

+0

@Inyavic鼠尾草此聲明和初始化INT第一** = {{5,7,4'-},{3,9,2},{7,3,6}}; (我認爲你的意思是int **而不是int first **)是不正確的。標量對象可以使用只有一個初始化器的支撐列表進行初始化。 –

回答

0

首先星星需要直接是一個類型的右側:

int** first; 

然後,你需要不同的初始化:

int** first = new int*[3]; 
    for (int i = 0; i < 3; i++) 
    { 
     first[i] = new int[3]; 
    } 
    first[0][0] = 5; 
    first[0][7] = 7; 
    // ... 
相關問題