0

I have a class template in C++ and another class that inherits it. The latter is not a class template, as you will see. The problem appears when I try to define the constructor of the derived class, by calling the constructor of the base class (the template one). I've posted the error below the code.

For simplicity's sake, I've only added the declarations. If you feel the code might help you get an idea of what the problem might be, I'll gladly post it.

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]

Any ideas?

conectionist
  • 2,694
  • 6
  • 28
  • 50
  • 2
    You don't have a "template class". You have a *class template*. Huge difference. In fact, this confusion is probably the single reason for your question. – Kerrek SB Sep 17 '12 at 14:07
  • 1
    OK, but now your question is kind of moot: You an only inherit from *classes*, not from *templates*... :-S – Kerrek SB Sep 17 '12 at 14:14
  • @KerrekSB or more to the point, you can inherit from class template *instantiations*; not from a template itself. Hard to inherit when there is no *there* there =P – WhozCraig Sep 17 '12 at 14:19
  • @KerrekSB: anyway, you're giving conectionist the runaround. If a "template class" is a class that has been instantiated from a template then you *can* inherit from a template class. I don't think that's an unreasonable usage and I've seen the term used that way, can't remember by whom. The problem wasn't the terminology, it's (in part) that due to TU issues the questioner *didn't* have a template class :-) – Steve Jessop Sep 17 '12 at 15:36
  • @SteveJessop: Quite right, you inherit from the template class, not the template. `State2D` is the class template, and `State2D` is the template class -- and that's what the OP needs in the initializer list. – Kerrek SB Sep 17 '12 at 16:01

1 Answers1

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

perhaps?

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • Now I'm getting this: undefined reference to `State2D::State2D(unsigned int, unsigned int, char const&)' – conectionist Sep 17 '12 at 14:08
  • Well, I do not see where it is defined. – Michael Krelin - hacker Sep 17 '12 at 14:09
  • 10
    @conectionist: Now it's time to dig up any of one billion duplicates on [why template defintions need to go in the header](http://stackoverflow.com/questions/3749099/why-should-the-implementation-and-the-declaration-of-a-template-class-be-in-the)... (see also the "Linked" column in that question) – Kerrek SB Sep 17 '12 at 14:09
  • 1
    templates will generate code for you, but they won't *write* it for you too. Again, confusion about the difference may well be a root of the problem. – WhozCraig Sep 17 '12 at 14:10
  • Well, yes, in the question you say "I've only added the definitions" when talking about declarations, so that might be where the problem is. – Michael Krelin - hacker Sep 17 '12 at 14:11
  • @MichaelKrelin-hacker My bad.. I wanted to say "declarations". I'll correct it. – conectionist Sep 17 '12 at 14:18
  • @KerrekSB Yeah, I putting the declarations and definitions it same file solved it. Thank you. – conectionist Sep 17 '12 at 14:33