【问题标题】:Does anyone know why it happens? C++ 'initializing': cannot convert from有谁知道为什么会这样? C++'初始化':不能从
【发布时间】:2020-01-11 15:34:22
【问题描述】:

所以这是我的课程标题:

#include <iostream>
class Board
{
public:
    Board();
    AllPieces* GetBoard();
    void createBoard();

protected:
    AllPieces** everything= new (AllPieces**)[32];

};

但是当我尝试初始化'everything'数组时,它说这个错误:

错误 C2440“正在初始化”:无法从“AllPieces ***”转换为“AllPieces **”

我不知道为什么这么说,我想做的是从类 AllPieces 中创建一个指针数组。

有人可以帮帮我吗?谢谢

【问题讨论】:

  • AllPieces** everything= new (AllPieces**)[32]; 应该是AllPieces** everything= new AllPieces*[32];
  • 相关:不要忘记与双指针相关的所有内容。使用 STL。
  • 你最好不要使用原始指针,而是使用智能指针 instdead。管理起来会容易得多。
  • 记住;更多的星星并不会让你成为更好的程序员——事实上,恰恰相反。

标签: c++ arrays class object debugging


【解决方案1】:

代码new (AllPieces**)[32] 创建了一个指向AllPieces** 类型对象的指针,因此该对象的类型为AllPieces***。如果您删除* 之一,您将获得正确的返回类型。

要消除错误,请将有问题的行更改为:

AllPieces** everything = new AllPieces*[32];

【讨论】:

  • 如果我这样做:AllPieces** Everything= new (AllPieces*)[32];新字下面有一条红线
  • @Heehee 你需要丢掉 () 只是 new AllPieces*[32]
猜你喜欢
  • 2021-09-22
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 2015-02-09
  • 1970-01-01
相关资源
最近更新 更多