我有一個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]
任何想法?
您沒有「模板類」。你有一個*類模板*。巨大的差異。事實上,這種混淆可能是你問題的唯一原因。 –
我改變了它... – conectionist
好的,但現在你的問題是有點沒有意義:你只從*類*繼承,而不是*模板* ...:-S –