如何将二维数组作为参数传递? [英] How to pass two-dimensional array as an argument?

查看:28
本文介绍了如何将二维数组作为参数传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Matrx 类定义为

class 矩阵{双 A[50][50];int m,n;民众:矩阵(空);Matrx(int a, int b){米=一;n=b;}矩阵运算符 +(矩阵 b);矩阵转置(矩阵 b);矩阵运算符 *(矩阵 b);CString printMatrx();void readMatrx(double a[][]);矩阵 TransposeMat(矩阵 b);};void Matrx::readMatrx(double a[][]){for(int i=0;i

智能感知给出如下错误

<块引用>

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

为什么?

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

解决方案

你需要正确地学习数组和指针.这包括哈!它们没有我想象的那么有用"这一课.在您熟悉了数组和指针的工作原理之后,您应该重新考虑您的设计.

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

#ifndef MATRIX_HPP_INCLUDED#define MATRIX_HPP_INCLUDED#include <向量>#include <算法>类矩阵{民众:typedef std::vector::size_type st;矩阵():行_(0),列_(0){}矩阵(int r,int c):rows_(r),cols_(c),coeffs_(st(r)* c,0.0){}无效重置(int r,int c){行_=r;cols_=c;coeffs_.clear();coeffs_.resize(st(r)*c,0.0);}int rows() const {return rows_;}int cols() const {return cols_;}双常数&operator()(int i, int j) const {return coeffs_[indexof(i,j)];}双&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)];}无效交换(矩阵和那个){std::swap(this->rows_,that.rows_);std::swap(this->cols_,that.cols_);this->coeffs_.swap(that.coeffs_));}私人的:int行_,cols_;std::vectorcoeffs_;st indexof(int i, int j) const {return st(i)*cols+j;}//行主存储};内联无效交换(矩阵& a,矩阵& b){a.swap(b);}矩阵&operator+=(matrix& lhs, matrix const& rhs);矩阵运算符+(matrix const& lhs, matrix const& rhs);矩阵运算符*(matrix const& lhs, matrix const& rhs);内联矩阵&operator*=(matrix& lhs, matrix const& rhs){ 矩阵 tmp = lhs * rhs;交换(tmp,lhs);返回 lhs;}...#万一

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

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

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

查看Definite C++ Book Guide 主题.>

My Matrx class is defined as

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];
  }
}

The intellisense gives error like the below

1 IntelliSense: an array may not have elements of this type d:\bmadaptive_dd_v1.02\matrx.h 17 27 TestServer

Why?

How to pass a two dimensional array as argument of the function?

解决方案

You need to properly learn about arrays and pointers. This includes the lesson "huh! They are not as useful as I thought they were". After you've gotten familiar with how arrays and pointers work exactly you should rethink your design.

For example, in my opinion, the following design has lots of advantages:

#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

This way you won't waste any space for small matrices, and you can support large matrices. Also, the use of std::vector instead of a pointer member which points to dynamically allocated memory removes the need to define your own copy constructor, assignment operator and destructor.

Of course, you could use boost::multi_array as a matrix replacement but using a custom matrix class allows you to declare overloaded operators in your own namespace which is desirable due to ADL (argument dependent lookup).

You might think that this doesn't really answer your question. In that case, let me stress that I think you don't fully understand how arrays and pointers work / behave. This is something you should look up in a decent C++ book. One could write many pages about this topic. You can't expect to see a short answer explaining all the quirks.

Check out the Definite C++ Book Guide thread.

这篇关于如何将二维数组作为参数传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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