2012-09-17 302 views
0

我有一個C++類模板和另一個繼承它的類。後者不是類模板,你會看到。當我嘗試通過調用基類的構造函數(模板之一)來定義派生類的構造函數時,會出現此問題。我在代碼下面發佈了錯誤。模板繼承

爲了簡單起見,我只添加了聲明。如果您覺得代碼可能會幫助您瞭解可能出現的問題,我很樂意將其發佈。

state2d.h

#ifndef STATE2D_H 
#define STATE2D_H 

template <typename T> 
class State2D 
{ 
public: 
    State2D(unsigned int _rows, unsigned int _columns); 
    State2D(unsigned int _rows, unsigned int _columns, const T& val); 
    State2D(const State2D<T> &st); 
    ~State2D(); 
    T& operator()(unsigned int i, unsigned int j); 
    const T& operator()(unsigned int i, unsigned int j) const; 
    unsigned int GetRowCount() const; 
    unsigned int GetColumnCount() const; 
    unsigned int GetAvailablePositionsCount() const; 

protected: 
    T** matrix; 
    unsigned int rows; 
    unsigned int columns; 
    unsigned int availablePositions; 
}; 

#endif // STATE2D_H 

TicTacToeState.h

#ifndef TICTACTOESTATE_H 
#define TICTACTOESTATE_H 

#include "state2d.h" 

class TicTacToeState : public State2D<char> 
{ 
public: 
    TicTacToeState(); 
}; 

#endif // TICTACTOESTATE_H 

TicTacToeState.cpp

#include "tictactoestate.h" 

TicTacToeState::TicTacToeState() : State2D(3,3,' ') // ERROR here; see below 
{ 
} 

error: class 'TicTacToeState' does not have any field named 'State2D' error: no matching function for call to 'State2D::State2D()' candidates are: State2D::State2D(const State2D&) [with T = char] State2D::State2D(unsigned int, unsigned int, const T&) [with T = char] State2D::State2D(unsigned int, unsigned int) [with T = char]

任何想法?

+2

您沒有「模板類」。你有一個*類模板*。巨大的差異。事實上,這種混淆可能是你問題的唯一原因。 –

+0

我改變了它... – conectionist

+1

好的,但現在你的問題是有點沒有意義:你只從*類*繼承,而不是*模板* ...:-S –

回答

10
: State2D<char>(3,3,' ') 

也許?

+0

現在我得到這個:未定義的引用'State2D :: State2D(無符號整型,無符號整型,字符常量&)' – conectionist

+0

嗯,我沒有看到它的定義。 –

+10

@conectionist:現在是時候挖掘任何十億重複[爲什麼模板定義需要在標題中](http://stackoverflow.com/questions/3749099/why-should-the-implementation-and- (參見該問題中的「Linked」列) –