开发者

Weird error in Turbo C++ IDE - Class template related question

Basically I'm trying to compile a template class which is meant to represent a table for adding up polynomials. As a result of this the table needs to be nullable.

This is the sort of thing I am trying to represent http://www.mathsisfun.com/algebra/polynomials-adding-subtracting.html.

And this is the template which is meant to do it:

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 开发者_StackOverflow社区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;

};

And this is the error I am getting:

[C++ Error] TableWithBlanks.h(16): E2034 Cannot convert 'TableWithBlanks::node ( *)[1]' to 'TableWithBlanks::node * *'

The PolynomialNode class is a class which is a linked list, where each node in the list represent the terms in a simple polynomial - I don't need to go into details.


In this line, you're trying to dynamically construct a two-dimensional array:

table_contents = new t_node[width][height];

but C++ doesn't work this way. For an example of how to allocate two-dimensional arrays, see this question, for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜