2013-12-11 52 views
0

,所以我想在我的類別矩陣超負荷運營商+(C++)使用此指令 M1 = M2 + M3需要幫助超載+

知道我已經超負荷個運營商的運營商=,但它崩潰在執行 這裏是我的代碼 類矩陣:

#include "../include/matrices.h" 
#include <iostream> 
using namespace std; 

class matrices 
{ 
    public: 
     int taille; 
     int **tab; 
    public: 
     matrices(); 
     matrices(int); 
     matrices(matrices &); 
     virtual ~matrices(); 
     void setTaille(int); 
     int getTaille(); 
     void setVal(int, int, int); 
     int getVal(int, int); 
     friend istream& operator >> (istream&, matrices&); //surcharge de l'opérateur >> 
     friend ostream& operator << (ostream&, const matrices&); //surcharge de l'opérateur << 
     //void operator=(matrices&); 
     matrices& operator=(matrices&); 
     //friend matrices& operator+(matrices , matrices); 
     friend matrices& operator+(matrices&, matrices&); 
     friend matrices operator-(matrices); 
     friend matrices operator*(matrices); 
}; 

matrices::~matrices() 
{ 
    //dtor 
} 

matrices::matrices() 
{ 
    taille = 2; 
    this->tab = new int*[taille]; 
    for(int i=0; i<taille; i++) 
     this ->tab[i] = new int[taille]; 
} 

matrices::matrices(int n) 
{ 
    taille = n; 
    this->tab = new int*[taille]; 
    for(int i=0; i<taille; i++) 
     this->tab[i] = new int[taille]; 
} 

matrices::matrices(matrices & m1) 
{ 
    (*this).taille = m1.taille; 
    tab = new int*[(*this).taille]; 
    for(int i=0; i<(*this).taille; i++) 
     for(int j=0; j<(*this).taille; j++) 
      this->tab[i][j] = m1.tab[i][j]; 
} 

void matrices::setTaille(int n) 
{ 
    (*this).taille = n; 
} 

int matrices::getTaille() 
{ 
    return (*this).taille; 
} 

void matrices::setVal(int val, int n, int m) 
{ 
    this->tab[n][m] = val; 
} 

int matrices::getVal(int n, int m) 
{ 
    return this->tab[n][m]; 
} 

istream& operator >> (istream& flux, matrices& m1) 
{ 
    for(int i=0; i<m1.taille; i++) 
    { 
     for(int j=0; j<m1.taille; j++) 
     { 
      flux >> m1.tab[i][j]; 
     } 
    } 
    return (flux); 
} 

ostream& operator << (ostream& flux, const matrices& m1) 
{ 
    for(int i=0; i<m1.taille; i++) 
    { 
     for(int j=0; j<m1.taille; j++) 
     { 
      flux << m1.tab[i][j]; 
      cout << "\t" ; 
     } 
     cout << "\n" << endl; 
    } 
    return (flux); 
} 

matrices& matrices::operator=(matrices& m1) 
{ 
    delete this->tab; 
    (*this).taille = m1.taille; 
    this->tab = new int*[m1.taille]; 
    for(int i=0; i<m1.taille; i++) 
     this->tab[i] = new int[m1.taille]; 

    for(int i=0; i<m1.taille; i++) 
    { 
     for(int j=0; j<m1.taille; j++) 
     { 
      this->tab[i][j] = m1.tab[i][j]; 
     } 
    } 
    return (*this); 
} 

matrices& operator+(matrices& m1, matrices& m2) 
{ 
    matrices mtmp; 
    mtmp.taille = m1.taille; 
    mtmp.tab = new int*[mtmp.taille]; 
    for(int i=0; i<mtmp.taille; i++) 
     mtmp.tab[i] = new int[mtmp.taille]; 

     if(m2.taille == m1.taille) 
     { 
      for(int i=0; i<mtmp.taille; i++) 
      { 
       for(int j=0; j<mtmp.taille; j++) 
       { 
        mtmp.tab[i][j] = m1.tab[i][j] + m2.tab[i][j]; 
       } 
      } 
     } 
     return mtmp; 
} 

,這是我的主要fonction

#include <iostream> 
#include "include/matrices.h" 
#include<stdlib.h> 
#include<stdio.h> 
#include<string> 

using namespace std; 

int main() 
{ 
    matrices m1,m2,m3; 
    cin >> m1; 
    cin >> m3; 
    m2 = m1; 
    cout << "=================================" << endl; 
    cout << "==== Affichage de la matrice ====" << endl; 
    cout << "=================================" << endl; 
    cout << m2; 
    getchar(); 

    cout << "==================================" << endl; 
    cout << "==== sommation des 2 matrices ====" << endl; 
    cout << "==================================" << endl; 
    m3 = m1 + m2; 
    cout << "==================================" << endl; 
    cout << "==== matrice m3 matrices est: ====" << endl; 
    cout << "==================================" << endl; 
    cout << m3; 
} 

感謝您的幫助

+2

不要你得到一個警告,關於參考返回到本地變量? –

+0

野應存在只是執行 – mtgenius

+0

期間crach你用調試器以確定現金在哪裏發生?如果是這樣,哪條線路導致崩潰?如果不是,爲什麼不? – Eric

回答

3

您必須是使用編譯器,不給你一個關於這個警告,或者忽略了警告:

matrices& operator+(matrices& m1, matrices& m2) 
{ 
    matrices mtmp; 
    ... 
    return mtmp; 
} 

的代碼是一個很大的問題:您嘗試返回一個自動變量的引用。當您嘗試使用該參考時,該對象不再存在。

你應該申報的過載是這樣的:

matrices operator+(const matrices& m1, const matrices& m2) 
{ 
    matrices mtmp; 
    ... 
    return mtmp; 
} 

這也將要求你改變你的朋友聲明:

friend matrices operator+(const matrices&, const matrices&); 

這樣可以幫助您返回副本(因爲你AREN不會改變m1m2,您應該聲明他們的引用爲const)。

此外,您應該查看複製分配操作符的copy-swap成語,因爲執行類似m1 = m1的操作會導致當前代碼崩潰。作爲這一點的必然結果:你不需要聲明一個拷貝構造函數,所以當你做一些像matricies m2 = m1這樣的工作時,它會做一個淺拷貝(拷貝構造函數的默認實現)。所以m2的指針指向m1中的數據。當你破壞m1時,m2包含懸掛指針......這將導致堆損壞(刪除已刪除的內存)。

很多你的內存問題(他們很多)只是通過切換到std::vectorstd::array(取決於你想如何定義你的類)而不是自己管理內存而消失。一個簡單(不完整)的例子:http://ideone.com/VVqvGy

+0

感謝傑克的解釋, ,但當我改變原型時,它給出了該錯誤: 錯誤:'m3'中'operator ='不匹配=(運算符+(常量矩陣&,常量矩陣&)(((常矩陣&)((常矩陣*)(&m2)))'| – mtgenius

+0

你改變了什麼? –

+0

我將矩陣&運算符+(矩陣&m1,矩陣&m2)更改爲矩陣運算符+(常量矩陣&m1,常量矩陣&m2)以及朋友聲明 – mtgenius

2

你的班級有很多問題。

  1. 您可以在構造函數中動態分配內存,但是您不需要在解構器中釋放內存 。這是內存泄漏。
  2. 在運算符=中,您可以使用new[]來分配內存,並使用delete(注意缺少[]):這是未定義的行爲,並且是隨機的。你也應該刪除所有動態分配的內存,而不僅僅是'頂層' - 就像你在構造函數的循環中分配它的方式一樣,你應該釋放它在一個循環中,並使用delete[]
  3. 您的setTaille方法毫無意義:它只會破壞您的數據,導致內存泄漏和數據不一致。它應該完全重寫,重新分配和複製當前矩陣的內容以允許設置或完全刪除大小。 (我真的不能想象它的用例)
  4. in operator+,你應該在執行任何分配之前檢查矩陣的大小是否相同。您應該決定添加不兼容矩陣的結果是什麼(這是一個艱難的選擇)。

在你的代碼錯誤很可能來自錯誤號2

編輯: 我發現,我忽略了另一個問題: 在你matrices& operator+功能,您返回到自動變量的引用(即是,在堆棧上的內存)。這是非法的:當你離開函數的時候,這個內存就變成了垃圾。你最想回到的構造矩陣的一個副本(使用matrices operator+

+0

謝謝divinas我會解決所有這些問題 – mtgenius