简体   繁体   English

进程退出,返回值 3221226356

[英]Process exited with return value 3221226356

I started to write simple class for matrix manipulation, one constructor fills a matrix with one number and other one takes 2d array as its argument.我开始编写简单的矩阵操作类,一个构造函数用一个数字填充矩阵,另一个将二维数组作为参数。

    #include <iostream>

using namespace std;
int i,j;
class Matrica
{
public:
    Matrica(int, int, double);
    Matrica(int, int, double*);
    ~Matrica();
    void saberi(Matrica, Matrica);
    void read();
    void print();
private:
    int n,m;
    double* matrix;  
};

Matrica::Matrica(int a, int b, double broj)
{
    n = a;
    m = b;
    matrix = new double[n*m];
    for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
            matrix[i*n + j] = broj;
        }
    }
}

Matrica::Matrica(int a, int b, double* matr)
{
    n = a;
    m = b;
    matrix = new double[n*m];
    for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
            matrix[i*n + j] = matr[i*n +j];
        }
    }

}
Matrica::~Matrica() {
    delete[] matrix;
}

void Matrica::read() {
    double e;
    for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
            cout<< "insert element ["<<i<<"]["<<j<<"] :" <<endl;
            cin >> e;
            matrix[n*i +j] = e;
            cout<< endl;
        }
    }
}

void Matrica::print() {
    for(i=0;i<n;i++) {
        for(j=0;j<m;j++) {
            cout << matrix[n*i + j] << " ";
        }
        cout << endl;
    }
}
void Matrica::saberi(Matrica A, Matrica B) {
    cout << "foo";
}

int main()
{

    Matrica A(3,3,1);
    double K[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    Matrica B(3,3,&K[0][0]);
    Matrica C(3,3,7);
    C.saberi(A,B);
    return 0;
}

My program worked ok before I added empty "saberi" method which takes two objects as arguments.在我添加以两个对象作为参数的空“saberi”方法之前,我的程序运行正常。 If I call it, my program crashes with 3221226356 return value.如果我调用它,我的程序将崩溃并返回 3221226356 返回值。 What could be wrong?可能有什么问题? Thanks in advance.提前致谢。

As you have defined your function signature正如你定义了你的函数签名

void saberi(Matrica, Matrica);

the Matrica objects there are passed by value, but you didn't provide a proper copy constructor for the class Matrica .那里的Matrica对象是按值传递的,但您没有为Matrica类提供适当的复制构造Matrica

The easiest way to solve your current problem, is to pass the parameters by reference解决当前问题的最简单方法是通过引用传递参数

void saberi(Matrica&, Matrica&);

or或者

void saberi(const Matrica&, const Matrica&);

Anyway, for the long term, you should either provide a proper copy constructor无论如何,从长远来看,您应该提供适当的复制构造函数

Matrica(const Matrica&);

or forbid it (just put the declaration in the private section of the class).或禁止它(只需将声明放在类的private部分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM