模板继承语法 [英] Template inheritance syntax
本文介绍了模板继承语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在玩C ++中的模板继承语法.我提出了以下内容,但不确定为什么会出现以下编译错误?
Hi,
I have been toying with template inheritance syntax in C++. I came up with the below but am unsure why I am getting the below compile error?
class myOutput
{
public:
void print()
{
std::cout << "hi from output\n";
}
};
template<class Output> class Svc
{
};
class SvcPrint : public Svc<class myOutput>
{
public:
void print()
{
// call the print function in myOutput
myOutput.print(); // SYNTAX ERROR !
} // error C2143: syntax error : missing '';'' before ''.''
};
我发表了我希望做的目标的评论.这不是一个实际的设计,只是玩弄语法.
这是在VS 2008上编译的.
感谢您提供任何信息.
I put in comments my objective of what I wish to do. This is not an actual design, just playing around with the syntax.
This was compiled on VS 2008.
Thanks for any information.
推荐答案
哦,我知道,我应该已经创建了对象的本地实例,就像这样...
Oh I see, I should have created a local instance of the object like this...
<pre lang="cs">class SvcPrint : public Svc<class myOutput><br />
{<br />
public:<br />
void print()<br />
{<br />
// call the print function in myOutput<br />
//myOutput.print();<br />
myOutput localObject;<br />
localObject.print();<br />
}<br />
};</pre><br />
现在可以了,因为myOutput引用了一个类型!
我可以这样使用它
Now its ok since the myOutput refers to a type!
And I can use it like this
int main()
{
SvcPrint a;
a.print();
return 0;
}
很好!
Nice!
这篇关于模板继承语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文