模板继承

时间:2012-09-17 14:03:09

标签: c++ templates inheritance constructor

我在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
{
}
  

错误:类'TicTacToeState'没有任何名为'State2D'的字段   错误:没有匹配函数来调用'State2D :: State2D()'   候选者是:State2D :: State2D(const State2D&amp;)[with T = char]                   State2D :: State2D(unsigned int,unsigned int,const T&amp;)[with T = char]                   State2D :: State2D(unsigned int,unsigned int)[with T = char]

有什么想法吗?

1 个答案:

答案 0 :(得分:10)

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

也许?