我正在为学校做一个项目,并且遇到了一些我想回答的问题。我最近问了一个关于同一个项目的问题here并解决了这个问题,但我现在面临另一个我无法解决的问题。所以我在这里再问一下我应该做些什么。
我创建了一个模板类,它包含一个固定长度的字符串,其中模板参数是字符串的长度,现在我正在尝试从模板类继承,以创建一个只接受数字并且是固定长度的字符串。我的问题是我无法使类定义正常工作。当我不使新类成为这样的模板时:
class DigitStr: public FixedStr<N>
它表示N是未声明的标识符。但是当我把它变成这样的模板时:
template <int N>
class DigitStr: public FixedStr<N>
然后,这些方法都无法正常工作,并提供未声明的标识符或需要模板参数。我尝试编写构造函数的方法是:
DigitStr::DigitStr ()
和
DigitStr<N>::DigitStr ()
所以有人在这看到问题吗?任何帮助将不胜感激。
答案 0 :(得分:2)
我不完全理解你的问题,但这可能会有所帮助:
template <int N>
class DigitStr: public FixedStr<N> {
DigitStr() {
// in-class constructor
}
~DigitStr();
};
template <int N>
DigitStr<N>::~DigitString() {
// out of class destructor
}
您需要使用this
来使用依赖于模板库的成员:
template <int N>
class DigitStr: public FixedStr<N> {
void foo() {
this->bar(); // call inherited member function
this->baz = 5; // set inherited member
}
};
答案 1 :(得分:0)
您需要在.CPP文件中重新定义模板类型:
template <int N>
DigitStr<N>::DigitStr (){
//your code here
}