【问题标题】:How to initzalize 3d matrix c++如何初始化 3d 矩阵 C++
【发布时间】:2015-01-02 13:11:05
【问题描述】:

我不知道如何初始化 3d 矩阵。

这就是终端类的3d矩阵:

contenidor ***_terminal;

这就是contenidor类的hpp:

class contenidor {

public:

  contenidor(const string &m, nat l) throw(error);

  contenidor(const contenidor &u) throw(error);

  ~contenidor() throw();

private:

   string _m;

   nat _l;

这就是我尝试初始化的方式:

contenidor _terminal = new contenidor**[n];
for(int x = 0; x < n; ++x) {
  _terminal[x] = new contenidor*[m];
  for(int y = 0; y < m; ++y) {
    _terminal[x][y] = new contenidor[y];
    for(int z = 0; z < h; ++z) { 
      _terminal[x][y][z] = 0;
    }
  }
}

终端显示的错误:

terminal.cpp: In constructor ‘terminal::terminal(util::nat, util::nat, util::nat, terminal::estrategia)’:
terminal.cpp:33:43: error: no matching function for call to ‘contenidor::contenidor()’
         _terminal[x][y] = new contenidor[y];
                                           ^
terminal.cpp:33:43: note: candidates are:
In file included from terminal.hpp:9:0,
                 from terminal.cpp:1:
contenidor.hpp:16:3: note: contenidor::contenidor(const contenidor&)
   contenidor(const contenidor &u) throw(error);
   ^
contenidor.hpp:16:3: note:   candidate expects 1 argument, 0 provided
contenidor.hpp:14:3: note: contenidor::contenidor(const string&, util::nat)
   contenidor(const string &m, nat l) throw(error);
   ^
contenidor.hpp:14:3: note:   candidate expects 2 arguments, 0 provided
terminal.cpp:35:8: error: ‘array’ was not declared in this scope
        array[x][y][z] = 0;
        ^

【问题讨论】:

标签: c++ arrays dynamic matrix 3d


【解决方案1】:

相信问题出在这里;你的两个构造函数是:

contenidor(const string &m, nat l) throw(error);
contenidor(const contenidor &u) throw(error);

但是您正在调用_terminal[x][y] = new contenidor[y];,它试图找到一个无参数构造函数contenidor()。要么创建一个不带参数的构造函数,要么将参数传递给_terminal[x][y] = new contenidor(...some arguments here...)[y];

【讨论】:

  • new contenidor(...some arguments here...)[y]; 在 C++ 中是无效语法。
  • 我试试 _terminal[x][y] = new contenidor(...some arguments here...)[y];并且不工作,请问sintax怎么样?
【解决方案2】:

问题是我有一个不可修改的 hpp,我不能添加 no-args 构造函数,即已回答的 sintax: _terminal[x][y] = new contenidor(...这里有一些参数...)[y]; 不正确

有人可以给我一个完整且正确的解决方案吗?

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多