【问题标题】:Weird error in Turbo C++ IDE - Class template related questionTurbo C++ IDE 中的奇怪错误 - 类模板相关问题
【发布时间】:2010-01-07 16:06:36
【问题描述】:

基本上,我正在尝试编译一个模板类,该类旨在表示一个用于将多项式相加的表格。因此,表需要可以为空。

这就是我试图代表http://www.mathsisfun.com/algebra/polynomials-adding-subtracting.html的那种东西。

这就是要做到这一点的模板:

template <class T> class TableWithBlanks : public Table<T> {
 public:

  TableWithBlanks( const int width, const int height ) : w(width), h(height), table_contents( new t_node[width][height]
  {
   table_contents = new t_node[width][height];
   // Go through all the values and blank them.
   for( int i = 0; i < w; i++)
   {
    for( int a = 0; a < h; a++)
    {
     table_contents[i][a].value_ptr = NULL;
    }
   }
  }

  void set_value( const int width, const int height, const T* table_value_ptr)
  {
   if( width <= w && height <= h )
   {
    table_contents[w][h] = table_value_ptr;
   }
  }

  T* get_value( const int width, const int height)
  {
   if( width <= w && height <= h )
   {
    return table_contents[width][height];
   }
  }

 private:
  typedef struct node {
   T* value_ptr;
  } t_node;

  t_node** table_contents;
  int w;
  int h;

};

这是我得到的错误:

[C++ 错误] TableWithBlanks.h(16): E2034 无法转换 'TableWithBlanks::node ( *)[1]' 到 'TableWithBlanks::node * *'

PolynomialNode 类是一个链表类,链表中的每个节点都表示一个简单多项式中的项 - 我不需要详细说明。

【问题讨论】:

    标签: c++


    【解决方案1】:

    在这一行中,您尝试动态构造一个二维数组:

    table_contents = new t_node[width][height];
    

    但 C++ 不能以这种方式工作。如何分配二维数组的示例,请参见this question,例如。

    【讨论】:

      猜你喜欢
      • 2018-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      相关资源
      最近更新 更多