2010-01-07 76 views
0

基本上我試圖編譯一個模板類,它是爲了表示一個表來添加多項式。由於這個表格需要可以爲空。Turbo C++ IDE中奇怪的錯誤 - 類模板相關問題

這就是我想表達的那種東西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 ::節點 (*)[1]' 以 'TableWithBlanks ::節點 *'

PolynomialNode類是一個鏈接列表,其中列表中的每個節點表示一個簡單多項式中的項 - 我不需要詳細說明。

回答

3

在這一行,你要動態地構造一個二維數組:

table_contents = new t_node[width][height]; 

但C++不以這種方式工作。例如,如何分配二維數組,請參閱this question

+0

+1 ..........很好抓:) :) – 2010-01-07 16:16:47

+0

謝謝 - 我沒有意識到C++可能是如此不直觀 – hairyyak 2010-01-07 16:36:29