请看看这个。我在问题函数和 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);
}
它不断抛出错误。