C ++函数不返回2D数组 [英] C++ function not returning 2d array

查看:84
本文介绍了C ++函数不返回2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个用于矩阵算术的Qt应用程序.如果选择int,则在x1 and y1中输入2个3x3数组,如果选择float,则在x2 and y2中输入2个3x3数组.看看MainWindow.cpp.

I am writing a Qt application for matrix arithmetic . I taken input of 2 3x3 arrays in x1 and y1 if int is selected and in x2 and y2 if float is selected . Have a look at MainWindow.cpp .

MainWindow.cpp

MainWindow.cpp

int x1[3][3],y1[3][3];
float x2[3][3],y2[3][3];
matrix<int> m1 ;
matrix<float> m2 ;
void MainWindow::addcheck(){
    typecheck();
    if(ui->intradio->isChecked()){
        int** add1 = m1.add(x1,y1);
        putresult1(add1);
    }
    else if(ui->floatradio->isChecked()){
        int** add2 = m2.add(x2,y2);
        putresult2(add2);
    }
}

我正在将这2个数组传递给矩阵类的add函数,如下所示.

I am passing these 2 arrays in add function of the matrix class as shown below .

matrix.h

template <class T>
class matrix
{
public:
    matrix();
    T** subtract(T**&,T**&);
    T** add(T**&,T**&);
    T** multiply(T**&,T**&);
};

matrix.cpp

matrix.cpp

template <class T>
T** matrix<T>::add(T**& a, T**& b) {
    T c[3][3] ;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            c[i][j] = a[i][j] + b[i][j] ;
        }
    }
    return c ;
}

但是,我在将2d数组返回**时收到了以下错误.我无法解密错误. 错误:

However, Im recieving the following error while returning the 2d array to ** . Im unable to decrypt the error . Error:

/Users/sarthakmunshi/prac/mainwindow.cpp:74: error: non-const lvalue reference to type 'int **' cannot bind to a value of unrelated type 'int [3][3]'
        add1 = m1.add(x1,y1);
                      ^~
/Users/sarthakmunshi/prac/matrix.h:10: passing argument to parameter here
    T** add(const T**&,const T**&);
                      ^

推荐答案

不管您以前听到过什么,数组都不是指针.二维数组与指向指针的指针无关,这是您的编译问题:无法从2D数组获取指向指针的指针.

Regardless of what you may have heard before, arrays are not pointers. A bidimensional array has little to do with a pointer to a pointer, which is your compile issue: there is no way of getting from a 2D array to a pointer-to-pointer.

如果数组的大小是固定的,则可以更改函数的签名以要求使用它(顺便说一句 build ,对,对吧?):

If your arrays are fixed size, you can change the signature of the functions to require it (and incidentally build, which is good, right?):

?? add(T (&a)[3][3], T (&b)[3][3]);

但是您还有什么可以返回的...答案当然是Matrix<T>,对吗?将两个矩阵相加会返回一个矩阵,这表明您正在寻找的函数更像(作为自由函数):

but you are left with what to return from there... of course the answer is Matrix<T>, right? Adding two matrices returns a matrix, which would indicate that the function you are looking for is more like (as a free function):

template <typename T>
Matrix<T> add(Matrix<T> const & lhs, Matrix<T> const & rhs);

当然可以拼写:

template <typename T>
Matrix<T> operator+(Matrix<T> const & lhs, Matrix<T> const & rhs);

我建议您创建一个最小的矩阵类(谷歌,您可以看到许多参考实现),或者使用现有库中的一个.

I would recommend that you create a minimal matrix class (google, you can see many reference implementations) or that you use one from an existing library.

这篇关于C ++函数不返回2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆