-8

请看看这个。我在问题函数和 main 中的相应调用中有评论。

#include<stdlib.h>
#include<iostream>
using namespace std;

void givesomespace()
{
    cout<<"------------------------NEW RESULTS BELOW------------------------"<<endl;
}


class squareMatrix 
{
public:

    squareMatrix();
    void makeEmpty(int n);
    void storeValue(int i, int j, int value);
    void print(int n);
    void add(squareMatrix matrix1, squareMatrix matrix2, squareMatrix result, int n);
    void subtract();
    void copy();
    int getvalue(int b,int c);

private:

    int matrix[10][10];
};

squareMatrix::squareMatrix()
{
}

void squareMatrix::makeEmpty(int n)
{
    for (int counter=0;counter<n;counter++)
    {
        for (int counter2=0;counter2<n;counter2++)
        {
            matrix[counter][counter2]=0;
        }
    }
}

void squareMatrix::print(int n)
{
    for (int counter=0;counter<n;counter++)
    {
        for (int counter2=0;counter2<n;counter2++)
        {
            cout<<matrix[counter][counter2]<<endl;
        }
    }
}

void squareMatrix::storeValue(int i, int j, int value)
{
    matrix[i][j]=value;
}

void squareMatrix::add(squareMatrix matrix1, squareMatrix matrix2, squareMatrix result,    int n)//-------------------PROBLEMS
{
    for (int counter=0;counter<n;counter++)
    {
        for (int counter2=0;counter2<n;counter2++)
        {
            result[counter][counter2] = (matrix1.getvalue(counter,counter2)+matrix2.getvalue(counter,counter2));
        }
    }
} //-----------------------------

void squareMatrix::subtract()
{

}

void squareMatrix::copy()
{

}

int squareMatrix::getvalue(int b, int c);
{
    return matrix[b][c];
}


main()
{
    squareMatrix matrix1,matrix2,result;
    matrix1.makeEmpty(5);
    matrix1.print(5);
    givesomespace();
    matrix1.storeValue(0,1,500);
    matrix1.print(5);
    givesomespace();
    matrix1.makeEmpty(5);
    matrix1.print(5);
    givesomespace();
    matrix2.storeValue(0,1,250);
    result.add(matrix1,matrix2,result,5); //------------corresponds with problem
    result.print(5);
    //result.subtract();
    //givesomespace();
    //result.print();
    //matrix2.storeValue(0,1,649)
    //matrix1.copy();
    //givesomespace();
    //matrix1.print();

    return(0);
}

它不断抛出错误。

4

2 回答 2

0
                     void squareMatrix::subtract(squareMatrix matrix1, squareMatrix matrix2, squareMatrix &result, int n)
                    {
                         int temp;
                        for (int counter=0;counter<n;counter++)
                        {
                            for (int counter2=0;counter2<n;counter2++)
                            {
                               temp=(matrix1.getvalue(counter,counter2)- matrix2.getvalue(counter,counter2));
                               result.storeValue(counter,counter2,temp);

                            }
                        }
                    }
于 2013-09-12T18:52:18.790 回答
0

您应该知道按值传递和按引用传递的概念,例如:

class A;
f(A& x);

使用 '&' 符号来声明这里的参数使用的是“自身”,而不是副本。

更多,如果你只是使用一个参数而不改变它的任何值。在参数类型之前使用 const。

例如:

void f(const int& x) { x = 0; }

这无法编译,因为 x 不能分配为 const 参数。

于 2013-09-11T20:39:09.747 回答