我在C ++中有一个类模板,而另一个类继承了它。正如您将看到的,后者不是类模板。当我尝试通过调用基类(模板一)的构造函数来定义派生类的构造函数时,就会出现问题。我已将错误发布到代码下方。

为了简单起见,我仅添加了声明。如果您认为代码可以帮助您了解问题的根源,我们将很乐意将其发布。

状态2d.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&)[with T = char]
                  State2D :: State2D(unsigned int,unsigned int,const T&)[with T = char]
                  State2D :: State2D(unsigned int,unsigned int)[with T = char]


有任何想法吗?

最佳答案

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


也许?

关于c++ - 模板继承,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12460960/

10-10 12:54