在C++中初始化后填充2D数组

在C++中初始化后填充2D数组,c++,arrays,C++,Arrays,我需要填充一个2D数组,它表示C++中函数中的一个矩阵。 函数如下所示: double** makeMatrix (const char c, double a) { double matrix[3][3]; switch (c) { case 'x' : matrix = {{1, 0, 0), {0, cos(a), -sin(a)}, {0, sin(a), cos(a)}}; break;

我需要填充一个2D数组,它表示C++中函数中的一个矩阵。

函数如下所示:

double** makeMatrix (const char c, double a) {
    double matrix[3][3];
    
    switch (c) {
        case 'x' :
            matrix = {{1, 0, 0), {0, cos(a), -sin(a)}, {0, sin(a), cos(a)}};
            break;
        case 'y' :
            matrix = {{cos(a), 0, sin(a)}, {0, 1, 0}, {-sin(a), 0, cos(a)}};
            break;
        case 'z' :
            matrix = {{cos(a), -sin(a), 0}, {sin(a), cos(a)}, {0, 0, 1}};
            break;
    }
    return matrix;
}
不用说,这个函数不起作用

我做错了什么

我显然有一个问题,因为我应该返回的变量类型与我正在返回的变量类型不匹配


我是。。。迷失在云端。

有一个紧急版本给你

#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void makeMatrix (const char c, const double a, double *mtx) {
    double matrix[3][3];
    for (int i=0; i<3; i++)
        for (int j=0; j<3; j++) matrix[i][j] = (i == j)? 1.0 : 0.0;
    switch (c) {
        case 'x' :
            matrix[1][1] = cos(a);
            matrix[1][2] = -sin(a);
            matrix[2][1] = sin(a);
            matrix[2][2] = cos(a);
            break;
        case 'y' :
            matrix[0][0] = cos(a);
            matrix[0][1] = -sin(a);
            matrix[2][0] = sin(a);
            matrix[2][2] = cos(a);
            break;
        case 'z' :
            matrix[0][0] = cos(a);
            matrix[0][1] = -sin(a);
            matrix[1][0] = sin(a);
            matrix[1][1] = cos(a);
            break;
    }
    std::copy_n(&matrix[0][0], 9, mtx);
    return;
}
int main()
{
    double mtx[3][3];
    makeMatrix('x', M_PI/3.0, &mtx[0][0]);
    for (int i=0; i<3; i++)
    {
        for (int j=0; j<3; j++) std::cout << mtx[i][j] << ",  ";
        std::cout << std::endl;
    }
}

这是一个包含构造函数的简单模板矩阵类。将其另存为tMatrix.h,并将其与.cpp文件一起放在工作目录中。

#ifndef __matrix_class___
#define __matrix_class___
#include <iostream>
#include <initializer_list>
#include <iomanip>
#include <complex>
template <typename T> class tMatrix
{
 public:
     T *ptr;
     int col, row, size;
     inline T* begin() const {return ptr;}
     inline T* end() const {return this->ptr + this->size;}
     inline T operator()(const int i, const int j)const { return ptr[i*col+j]; }
     inline T&operator()(const int i, const int j) { return ptr[i*col+j]; }
     inline tMatrix(): col{0}, row{0}, size{0}, ptr{0} {;}
     tMatrix(const int i, const int j): col(j), row(i), size(i*j)
     {
         ptr = new T [this->size] ;
     }
     tMatrix(const std::initializer_list< std::initializer_list<T> > s):tMatrix<T>(s.size(), s.begin()->size())
     {
        int j = 0;
       for (const auto& i : s) {  std::copy (i.begin(), i.end(), ptr + j*col); ++j ; }
     }
     tMatrix(const tMatrix<T>&a) : tMatrix<T>(a.row, a.col)
     {
         std::copy(a.begin(), a.end(), this->ptr);
     }
     tMatrix& operator=(const tMatrix<T>&a)
     {
         std::copy(a.begin(), a.end(), this->ptr);
         return *this;
     }
    ~tMatrix() {delete [] this->ptr;}
 };
 template <typename X> std::ostream& operator<<(std::ostream&p, const tMatrix<X>&a)
{
    p << std::fixed;
    for (int i=0; i<a.row; i++) {
        for (int j=0; j <a.col; j++) p << std::setw(12) << a(i, j);
        p << std::endl;
    }
    return p;
}
using iMatrix = tMatrix<int>;
using rMatrix = tMatrix<double>;
using cMatrix = tMatrix<std::complex<double> >;
#endif
然后将您的程序修改为:

#include <cmath>
#include "tMatrix.h"
using namespace std;
rMatrix makeMatrix (const char c, double a) {
   rMatrix mtx(3,3);
   double cs = cos(a), ss = sin(a);
   switch (c) {
       case 'x' :
          mtx = rMatrix( { {1, 0, 0}, {0, cs, -ss}, {0, ss, cs} } );
          break;
       case 'y' :
          mtx = rMatrix( { {cs, 0, ss}, {0, 1, 0}, {-ss, 0, cs} } );
          break;
       case 'z' :
          mtx = rMatrix( { {cs, -ss, 0}, {ss, cs, 0}, {0, 0, 1} } );
          break;
       default:
          break;
     }
   return mtx;
}
int main()
{
  char axis = 'x';
  double angle = M_PI / 180. * 60.;
  rMatrix mtx = {{0,0,0}, {0,0,0}, {0,0,0}}; //initial a 3x3 matrix with all 0s
  std::cout << mtx << std::endl;
  std::cout << makeMatrix(axis, angle) <<std::endl;
  return 0;
}

除了你正在重新调整悬空指针OOP之外,不要试图猜测C++语法。逐个指定元素。顺便说一句,从数学角度来看,你的z案例看起来很有趣。实际上还有其他错误吗?看起来你滑出了几个标志。行列式是1吗?你检查-我太老了。即使你返回一个指向堆栈对象的指针也表明你确实丢失了。忘记所有这些,找一本好书并使用std::vector。这是重复的C代码。C++的答案很可能是使用向量。在我看来,答案是使用线性代数库中的矩阵类:我使用BLAS,它是Boost分布的一部分。如果是这样的话,你将要做矩阵数学,你应该使用线性代数库中的矩阵类来简化你的生活;看起来像UB,虽然它可能会工作。谢谢你,但实际上我是为了更聪明的东西,因为我想避免键入几个矩阵[I][j]。我还对学习一般操作感兴趣,而不仅仅是如何解决这个问题。在初始化之后,必须有某种方法在一行中填充矩阵,不是吗?但是,无论如何,谢谢你@我明白了。我上了一堂矩阵课,但这对于学习来说太大了。我可以给你做一个小的。对矩阵使用初始化列表绝对不是一件简单的工作。您现在可以把它收起来。@JimmyScionti使用std::initializer\u list对矩阵进行括号括起来的初始化。您必须使用构造函数编写一个矩阵类,该构造函数将嵌套的std::initializer\u列表作为参数。现在有点复杂了。@TedLyngmo“谢谢”。它是“伟大的”。