2

I'm working on a project for school and am running into some problems I would like to have answered. I recently asked a question here about the same project and got that problem solved but am now facing another problem that I don't have the knowledge to fix. So I'm here asking again what I should do.

I created a template class that holds a fixed length string where the template argument is the length of the string and now I'm trying to inherit from the template class to make a string that only accepts digits and is a fixed length. My problem is that I can't get the class definition to work correctly. When I don't make the new class a template like this:

class DigitStr: public FixedStr<N>

It says that N is an undeclared identifier. However when I make it a template like this:

template <int N>
class DigitStr: public FixedStr<N>

Then none of the methods work correctly and give either an undeclared identifier or requires template argument. The ways I've tried writing the constructors were:

DigitStr::DigitStr ()

and

DigitStr<N>::DigitStr ()

So does anyone see the problem here? Any help would be greatly appreciated.

Community
  • 1
  • 1
triple07
  • 63
  • 9

2 Answers2

2

I don't fully understand your problem, but this may help:

template <int N>
class DigitStr: public FixedStr<N> {
  DigitStr() {
    // in-class constructor
  }
  ~DigitStr();
};

template <int N>
DigitStr<N>::~DigitString() {
  // out of class destructor
}

You need to use this to use members dependent on the template base:

template <int N>
class DigitStr: public FixedStr<N> {
  void foo() {
    this->bar(); // call inherited member function
    this->baz = 5; // set inherited member
  }
};
Pubby
  • 51,882
  • 13
  • 139
  • 180
0

You need to redefine the template type in your .CPP file:

template <int N>
DigitStr<N>::DigitStr (){
  //your code here
}
Radix
  • 1,317
  • 2
  • 17
  • 32