基本上,我正在尝试编译一个模板类,该类旨在表示用于添加多项式的表。因此,该表必须可以为空。
这是我要代表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类是一个链接列表的类,该列表中的每个节点都代表一个简单多项式中的项-我无需赘述。
最佳答案
在此行中,您尝试动态构造一个二维数组:
table_contents = new t_node[width][height];
但是C ++不能这样工作。有关如何分配二维数组的示例,请参见this question。