如何将二维数组作为参数传递?

时间:2011-01-26 08:50:40

标签: c++ arrays multidimensional-array

我的Matrx课程定义为

class Matrx
{
 double A[50][50];
 int m,n;
public:
 Matrx(void);
 Matrx(int a, int b)
 {
  m=a;
  n=b;
 }
 Matrx operator +(Matrx b);
 Matrx Transpose(Matrx b);
 Matrx operator *(Matrx b);
 CString printMatrx();
 void readMatrx(double a[][]);
 Matrx TransposeMat(Matrx b);
};

void Matrx::readMatrx(double a[][])
{
 for(int i=0;i< m;i++)
  {
   for(int j=0;j< n;j++)
    A[i][j]=a[i][j];
  }
}

intellisense给出如下所示的错误

  

1 IntelliSense:数组可能没有此类型的元素d:\ bmadaptive_dd_v1.02 \ matrx.h 17 27 TestServer

为什么?

如何传递二维数组作为函数的参数?

6 个答案:

答案 0 :(得分:25)

问题是当在C ++中将多维数组作为参数传递时,必须指定最外层数组的维度。例如:

void ThisIsIllegal(int arr[][]); // Wrong!
void ThisIsAlsoIllegal(int arr[10][]); // Also wrong
void ThisIsLegal(int arr[][10]); // Okay

如果您希望能够使用任何大小的数组,您可以使用模板:

template <size_t N, size_t M>
void ThisIsAlsoLegal(int (&arr)[M][N]);

这最后一个版本接受任何正确类型的多维数组,可能正是您所寻找的。

答案 1 :(得分:4)

您需要正确了解数组和指针。这包括“嗯!它们没有我想象的那么有用”的教训。在熟悉了数组和指针的工作原理之后,你应该重新考虑你的设计。

例如,在我看来,以下设计有很多优点:

#ifndef MATRIX_HPP_INCLUDED
#define MATRIX_HPP_INCLUDED

#include <vector>
#include <algorithm>

class matrix
{
public:
    typedef std::vector<double>::size_type st;
    matrix() : rows_(0), cols_(0) {}
    matrix(int r, int c) : rows_(r), cols_(c), coeffs_(st(r)*c,0.0) {}
    void reset(int r, int c)
    { rows_=r; cols_=c; coeffs_.clear(); coeffs_.resize(st(r)*c,0.0); }
    int rows() const {return rows_;}
    int cols() const {return cols_;}
    double const& operator()(int i, int j) const {return coeffs_[indexof(i,j)];}
    double      & operator()(int i, int j)       {return coeffs_[indexof(i,j)];}
    double const* operator[](int i) const {return &coeffs_[indexof(i,0)];}
    double      * operator[](int i)       {return &coeffs_[indexof(i,0)];}
    void swap(matrix& that)
    {
      std::swap(this->rows_,that.rows_);
      std::swap(this->cols_,that.cols_);
      this->coeffs_.swap(that.coeffs_));
    }
private:
    int rows_, cols_;
    std::vector<double> coeffs_;
    st indexof(int i, int j) const {return st(i)*cols+j;} // row major storage
};

inline void swap(matrix& a, matrix& b) {a.swap(b);}

matrix& operator+=(matrix& lhs, matrix const& rhs);
matrix operator+(matrix const& lhs, matrix const& rhs);
matrix operator*(matrix const& lhs, matrix const& rhs);
inline matrix& operator*=(matrix& lhs, matrix const& rhs)
{ matrix tmp = lhs * rhs; swap(tmp,lhs); return lhs; }
...

#endif

这样您就不会为小矩阵浪费任何空间,并且您可以支持大型矩阵。此外,使用std :: vector而不是指向动态分配的内存的指针成员,无需定义自己的复制构造函数,赋值运算符和析构函数。

当然,您可以使用boost :: multi_array作为矩阵替换,但使用自定义矩阵类允许您在自己的命名空间中声明重载运算符,这是由于ADL(参数相关查找)所需。

您可能认为这并不能真正回答您的问题。在这种情况下,让我强调一下,我认为你并不完全理解数组和指针是如何工作/表现的。这是你应该在一本体面的C ++书中查找的内容。人们可以写很多关于这个主题的网页。你不能期望看到解释所有怪癖的简短答案。

查看Definite C++ Book Guide主题。

答案 2 :(得分:1)

您需要指定除第一个维度之外的所有维度,例如

void Matrx::readMatrx(double a[][50])

从这里你可以看到你对你的类的实现方式存在根本问题,但至少你现在应该能够编译代码了。

答案 3 :(得分:0)

您可以指定所有尺寸或仅指定最后一个尺寸来传递数组:

void Matrx::readMatrx(double a[][50])
{
 for(int i=0;i< m;i++)
  {
   for(int j=0;j< n;j++)
    A[i][j]=a[i][j];
  }
}

答案 4 :(得分:0)

感谢所有人......你真的在帮助......我发现自己有了一个新的类定义和适合我需要的矩阵的函数定义。我需要你评论它......下面是类声明的例子..

#include<iostream>
#include"stdafx.h"
using namespace std;  
const int M=100; const int N=100;  
class Matrix  
{  
    double A[M][N];  
    int m,n;  
public:  
    Matrix(void);  
    Matrix(int a, int b)  
    {  
        m=a;  
        n=b;  
        for(int i=0;i<m;i++)  
        {  
            for(int j=0;j<m;j++)  
                A[i][j]=0.0;  
        }  
    }  
    Matrix operator +(Matrix b);  
    Matrix operator -(Matrix b);  
    Matrix operator *(Matrix b);  
    void TransPose(Matrix b);  
    CString printMatrix();  
    void readMatrix(double*);  
};

然后是函数实现

#include "stdafx.h"
#include "Matrix.h"

Matrix::Matrix(void)  
{  
}  

Matrix Matrix::operator *(Matrix b)  
{  
 Matrix c(m,m);  
 if(n!=b.m)  
 {  
    HWND hndOwner(0);   
    MessageBoxW(hndOwner,L"Multiplication not possible row and column does not match",L"Error",NULL);  
    Matrix errMat(1,0);  
    double er[1][1]={0}; errMat.readMatrix((double *)er);  
    return errMat;  
 }  
for(int i=0;i< m;i++)  
{  
for(int k=0;k< m;k++)  
{  
c.A[i][k]=0;  
for(int j=0;j< n;j++)  
{  
c.A[i][k] = c.A[i][k]+(A[i][j] * b.A[j][k]) ;  
}  
}  
}  
return c;  
}  
Matrix Matrix::operator +(Matrix b)  
{  
    Matrix c(m,n);  
    if((n!=b.n)||(m!=b.m))  
 {  
    HWND hndOwner(0);   
    MessageBoxW(hndOwner,L"Addition not possible row and column does not match",L"Error",NULL);  
    Matrix errMat(1,0);  
    double er[1][1]={0}; errMat.readMatrix((double *)er);  
    return errMat;  
 }  
    for(int i=0;i<m;i++)  
        {  
            for(int j=0;j< n;j++)  
                {  
                    c.A[i][j]=0.0;  
                }  
        }  
        for(int i=0;i< m;i++)  
        {  
            for(int j=0;j< n;j++)  
                {  
                    c.A[i][j]=A[i][j]+b.A[i][j];  
                }  
        }  
return c;  
}    
CString Matrix::printMatrix()  
{  
    CString strB(" "),strDisp;  
    for(int iRow=0;iRow<m;iRow++)  
    {  
        for(int iColumn=0;iColumn<n;iColumn++)  
        {  
            strB.Format(L"%lf ",A[iRow][iColumn]);  
            strDisp+=strB;  
        }  
            strDisp+="\r\n";  
    }  
        return strDisp;  
}  
void Matrix::readMatrix(double *ptrarr)  
{  
    for(int i=0;i< m;i++)  
        {  
            for(int j=0;j< n;j++)  
                A[i][j]=*(ptrarr+i*n+j);  
        }  
}  
void Matrix::TransPose(Matrix b)  
{  
    for(int i=0;i<b.n;i++)  
    {  
        for(int j=0;j<b.m;j++)  
            A[i][j]=b.A[j][i];  
    }  
}   

以上简单的代码到目前为止运作良好...如果您对改进有任何建议,请建议..

答案 5 :(得分:-1)

你很幸运,因为:

for(int j=0;j<m;j++)
    A[i][j]=0.0;

应该是:

for(int j=0;j<**n**;j++)
    A[i][j]=0.0;
相关问题