请看一下这个。我在问题函数和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);
}
它不断抛出错误。
答案 0 :(得分:0)
您应该知道按值传递的概念并通过引用传递,例如:
class A;
f(A& x);
使用'&amp;'签署声明这里参数使用“本身”,而不是副本。
更多,如果您只使用参数而不更改任何值。在参数类型之前使用const。
e.g:
void f(const int& x) { x = 0; }
这是无法编译的,因为x不能被指定为const参数。
答案 1 :(得分: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);
}
}
}