2017-07-12 73 views
1

我是C++的新手,一般編程很好。我正在嘗試在C++中學習語法,並且我正在嘗試通過類打印6X6矩陣。我附上了下面的代碼。我應該得到一個充滿零的6X6矩陣,但我正在得到一些其他的價值。如果直接從main()打印,我沒有這個問題。請查看代碼並進行放附加以下(矩陣C和B)二維數組通過C++中的類

感謝,

#include <iostream> 

class test { 
public: 
    test(); 
    ~test() {}; 

    int c[6][6]; 

    int print(); 
}; 

test::test() { 
    int c[6][6] = { 0 }; 
} 

int test::print() { 
    for (int r = 0; r < 6; r++) { 
     for (int q = 0; q < 6; q++) { 
      cout << c[r][q] << " "; 
     }cout << endl; 
    } 

    return 0; 
} 


int main() 
{ 
    int B[4][4] = { 0 }; 
    test a; 
    a.print(); 

    std::cout << endl; 
    for (int i = 0; i < 4; i++) { 
     for (int j = 0; j < 4; j++) { 
      std::cout << B[i][j] << " "; 
     } 
     std::cout << std::endl; 
    } 
    std::cout << std::endl; 

    return 0; 
} 

程序的輸出:

-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 

0 0 0 0 
0 0 0 0 
0 0 0 0 
0 0 0 0 
+2

的構件'INT C [6] [6];''中是test'未初始化 –

+0

大!如果我用函數定義初始化它c [6] [6] = {0},它會起作用。不應該這樣做,因爲我已經在構造函數下初始化它了嗎?我被告知使用構造函數進行初始化。我錯過了什麼? – user6771484

+0

查看我發佈的答案。 –

回答

2

的問題

眼前的問題是,你的構造包括線

int c[6][6] = {0}; 

但是,這shadowsc,和因此,您分配的值不會傳播到類作用域。

檢測問題

你可以爲自己容易想通了這一點......如果你打開了警告你的編譯器。 (您可能不知道:在未來使用的警告,你的優勢!)

通過與編譯代碼:

g++ temp2.cpp -Wall 

我得到下面的實用信息:

temp2.cpp: In constructor ‘test::test()’: 
temp2.cpp:15:9: warning: unused variable ‘c’ [-Wunused-variable] 
    int c[6][6] = { 0 }; 

指示需要注意的確切路線,雖然也許不是問題的症狀(編譯器擔心在本函數中本地不使用c,但也許應該已經就影子變量提出了警告 - 哦)。

提高代碼

由於您使用C++,你可以採取std::vector類的優勢來處理你的陣列的存儲管理。您也可以聲明您的printconst以防止它更改c的值的任何可能性。將這些變化,存取方法和平面陣列索引沿着給出以下:

#include <iostream> 
#include <vector> 

class Test { 
public: 
    std::vector<int> c; 
    int width; 
    int height; 

    Test() = default; 

    Test(int width0, int height0, int init_val=0){ 
    width = width0; 
    height = height0; 
    c.resize(width*height,init_val); 
    } 

    int operator()(int x, int y) const { 
    return c[y*width+x]; 
    } 

    int& operator()(int x, int y) { 
    return c[y*width+x]; 
    } 

    void print() const { 
    for(int y=0;y<height;y++){ 
     for(int x=0;x<width;x++) 
     std::cout<<operator()(x,y)<<" "; 
     std::cout<<std::endl; 
    } 
    } 
}; 

int main(){ 
    Test t(6,6,0); 
    t.print(); 

    return 0; 
} 
0

在你的構造:

test::test() { 
    int c[6][6] = {0}; 
} 

你正在聲明一個名爲c的數組,初始化爲0。但是,類成員c未初始化。你大概意思是:

test::test() : c{} { 
} 

甚至更​​好,這一點:

class test { 
public: 
    test() = default; 
    ~test() = default; 
    int c[6][6] = {}; 
    int print(); 
}; 
+0

從構造函數初始化,如c = {0};導致錯誤「array type int [6] [6] is not assignable」,but initialize it as test :: test(){ \t for(int i = 0; i <6; i ++){ \t int j = 0; j <6; j ++){ \t \t \t c [i] [j] = 0; \t \t} \t} }起作用。有沒有更好的方法通過構造函數初始化它?按照您的建議直接在聲明過程中進行初始化! – user6771484

+0

@ user6771484使用構造函數初始值設定項列表:'test :: test():c {/*...*/} {}'。 – DeiDei

+0

@ user6771484對不起,試試我的編輯。 –

2

test::test constuctor正在初始化一個新的局部變量,你由此值分配給一個新的變量,而不是你的類的。 刪除INT初始化之前和大小應該修復它

+0

它不僅消除了類型說明符'int',但也消除了數組的大小。 –

+0

是的,我的壞,編輯出來 – user8271377

0
test::test() 
{ 
    // declaring a local variable 
    int c[6][6] = { 0 }; 

    // initializing the actual matrix 
    for (int r = 0; r < 6; r++) 
     for (int q = 0; q < 6; q++) 
      this->c[r][q] = 0; 

    // this is a pointer to the object 
    // local c & this->c are two different variables 
}