2013-10-22 98 views
0

首先我知道乘法部分是错误的,但我有一些关于代码的问题。

1.当我超载我的操作员+我打印出矩阵使用cout < < *这然后正确后我返回*这和当我做a + b对matix a和matix b它不给我同样的事情非常混乱。


2.当我制作矩阵C在我的主要我不能使用我的默认构造函数出于某种原因,因为当我去设置它=使用我的赋值操作符重载函数它给了我一个错误说:“表达式必须是可修改的虽然使用我的构造函数来设置行号和列号与使用(0,0)的默认构造函数相同

3.我的赋值operator = function使用复制构造函数使用值创建一个新矩阵在等号的右侧,当我打印出c它不会给我任何东西

任何帮助将是伟大的这是我的hw的算法类,我仍然需要做的算法的乘法矩阵,但我需要先解决这些问题,我有很多麻烦,请帮助。C++赋值运算符动态数组

//Programmer: Eric Oudin 
//Date:   10/21/2013 
//Description: Working with matricies 

#include <iostream> 
using namespace std; 

class matrixType 
{ 
public: 
    friend ostream& operator<<(ostream&, const matrixType&); 

    const matrixType& operator*(const matrixType&); 
    matrixType& operator+(const matrixType&); 
    matrixType& operator-(const matrixType&); 
    const matrixType& operator=(const matrixType&); 

    void fillMatrix(); 
    matrixType(); 
    matrixType(int, int); 
    matrixType(const matrixType&); 
    ~matrixType(); 
private: 
    int **matrix; 
    int rowSize; 
    int columnSize; 
}; 
ostream& operator<< (ostream& osObject, const matrixType& matrix) 
{ 
    osObject << endl; 
    for (int i=0;i<matrix.rowSize;i++) 
    { 
     for (int j=0;j<matrix.columnSize;j++) 
     { 
      osObject << matrix.matrix[i][j] <<", "; 
     } 
     osObject << endl; 
    } 
    return osObject; 
} 
const matrixType& matrixType::operator=(const matrixType& matrixRight) 
{ 
    matrixType temp(matrixRight); 
    cout << temp; 
    return temp; 
} 
const matrixType& matrixType::operator*(const matrixType& matrixRight) 
{ 
    matrixType temp(rowSize*matrixRight.columnSize, columnSize*matrixRight.rowSize); 
    if(rowSize == matrixRight.columnSize) 
    { 
     for (int i=0;i<rowSize;i++) 
     { 
      for (int j=0;j<columnSize;j++) 
      { 
       temp.matrix[i][j] = matrix[i][j] * matrixRight.matrix[i][j]; 

      } 
     } 
    } 
    else 
    { 
     cout << "Cannot multiply matricies that have different size rows from the others columns." << endl; 
    } 
    return temp; 
} 
matrixType& matrixType::operator+(const matrixType& matrixRight) 
{ 
    matrixType temp; 
    if(rowSize == matrixRight.rowSize && columnSize == matrixRight.columnSize) 
    { 
     temp.setRowsColumns(rowSize, columnSize); 
     for (int i=0;i<rowSize;i++) 
     { 
      for (int j=0;j<columnSize;j++) 
      { 
       temp.matrix[i][j] = matrix[i][j] + matrixRight.matrix[i][j]; 
      } 
     } 
    } 
    else 
    { 
     cout << "Cannot add matricies that are different sizes." << endl; 
    } 
    return temp; 
} 
matrixType& matrixType::operator-(const matrixType& matrixRight) 
{ 
     matrixType temp(rowSize, columnSize); 
    if(rowSize == matrixRight.rowSize && columnSize == matrixRight.columnSize) 
    { 
     for (int i=0;i<rowSize;i++) 
     { 
      for (int j=0;j<columnSize;j++) 
      { 
       matrix[i][j] -= matrixRight.matrix[i][j]; 

      } 
     } 
    } 
    else 
    { 
     cout << "Cannot subtract matricies that are different sizes." << endl; 
    } 
    return *this; 
} 
void matrixType::fillMatrix() 
{ 
    for (int i=0;i<rowSize;i++) 
    { 
     for (int j=0;j<columnSize;j++) 
     { 
      cout << "Enter the matix number at (" << i << "," << j << "):"; 
      cin >> matrix[i][j]; 
     } 
    } 
} 
matrixType::matrixType() 
{ 
    rowSize=0; 
    columnSize=0; 
    matrix = new int*[rowSize]; 
    for (int i=0; i < rowSize; i++) 
    { 
     matrix[i] = new int[columnSize]; 
    } 

} 
matrixType::matrixType(int setRows, int setColumns) 
{ 
    rowSize=setRows; 
    columnSize=setColumns; 
    matrix = new int*[rowSize]; 
    for (int i=0; i < rowSize; i++) 
    { 
     matrix[i] = new int[columnSize]; 
    } 
} 
matrixType::matrixType(const matrixType& otherMatrix) 
{ 
    rowSize=otherMatrix.rowSize; 
    columnSize=otherMatrix.columnSize; 
    matrix = new int*[rowSize]; 
    for (int i = 0; i < rowSize; i++) 
    { 
     matrix[i]=new int[columnSize]; 
     for (int j = 0; j < columnSize; j++) 
     { 

      matrix[i][j]=otherMatrix.matrix[i][j]; 
     } 
    } 
} 
matrixType::~matrixType() 
{ 
    delete [] matrix; 
} 
int main() 
{ 
    matrixType a(2,2); 
    matrixType b(2,2); 
    matrixType c(0,0); 
    cout << "fill matrix a:"<< endl;; 
    a.fillMatrix(); 
    cout << "fill matrix b:"<< endl;; 
    b.fillMatrix(); 

    cout << a; 
    cout << b; 

    //c = a+b; 

    cout <<"matrix a + matrix b =" << a+b; 

    system("PAUSE"); 
    return 0; 
} 




编辑:
仍然有事情不返回什么,我告诉它返回

回答

0
  1. 此代码不会做你认为它的麻烦:

    for (int j = 0; j < columnSize; j++) 
    { 
        matrix[i]=new int[columnSize]; 
        matrix[i][j]=otherMatrix.matrix[i][j]; 
    } 
    

    它分配一个列,然后复制第一个值。然后它分配另一列(忘记旧列),并复制第二个值。然后分配另一列(再次忘记旧列),并复制第三个值。

    我希望这个问题很清楚,从这个描述本身。

  2. 您是否试图使用matrixType c();?那会宣布一个功能。使用默认构造函数的正确语法是matrixType c;

  3. 您的运营商=并没有实际分配任何东西。所以c = a+b;计算a+b但不会更改c

+0

是的我认为我通过将矩阵[i] =新的int linw移动到2以上的for循环中来帮助我很好地感谢,如果我修复1将3工作? – CrimOudin

+0

仍然没有回报我认为他们应该返回的东西的麻烦 – CrimOudin