c++ 中的类模板内的模板类
这里的菜鸟仍在尝试模板。 尝试写一个消息处理类模板
template <typename T> class MessageProcessor {
//constructor, destructor defined
//Code using t_ and other functions
foo( void ) {
//More code in a perfectly fine method
}
private: T *t_
};
全部定义在头文件中。 我已经构建并测试了我的类,一切都很好。 现在,我正在尝试这样做:
template <typename T> class MessageProcesor {
//Same stuff as before
foo(void) {
//Same code as before in foo, but one new line:
t_->getMessageSender<MessageType>();
}
private: T *t_;
};
但是,这一行在 '>' 之前给了我错误的表达式类型错误 令牌。
我添加了必要的头文件来定义 MessageType 是什么。 我以前多次使用过这个函数,只是不是在这个上下文中。
我怀疑编译器不喜欢模板函数在未定义的类模板(非专业化?)中完全定义(专业化?)。 我还没有完全理解是什么让模板“专业化”。 大多数解释都集中在“完整”或“部分”的概念上,但并不是它首先专业化的原因。
如果您想查看更多代码,我们深表歉意。 我在工作时无法访问互联网,而这就是我所做的事情,所以我必须将所有内容放入我的精神“便签本”中并将其带回家。
noob here still experimenting with templates. Trying to write a message processing class template
template <typename T> class MessageProcessor {
//constructor, destructor defined
//Code using t_ and other functions
foo( void ) {
//More code in a perfectly fine method
}
private: T *t_
};
All defined in a header file. I've built and tested my class and all is well. Now, I'm trying to do this:
template <typename T> class MessageProcesor {
//Same stuff as before
foo(void) {
//Same code as before in foo, but one new line:
t_->getMessageSender<MessageType>();
}
private: T *t_;
};
However, this line gives me an error of bad expression-type before '>' token.
I've added the necessary header files to define what a MessageType is. I've used this function many time before, just not in this context.
I suspect that the compiler doesn't like the fact that the template function is fully defined (specialized?) within an undefined class template (unspecialized?). I'm not fully grokking what makes a template 'specialized'. Most explanations center on the concepts of 'full' or 'partial', but not what makes it specialized in the first place.
Apologies if you'd like to see more code. I have no internet access at work and that's where I'm doing this, so I have to put everything into my mental 'scratchpad' and bring it home.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用关键字“template”
您的成员函数“foo”需要一个返回类型,并且当您在依赖表达式(其含义直接或间接依赖于通用模板参数的表达式)中使用成员模板时, 成员模板需要以“template”关键字作为前缀[注意:为了对称起见,您可以始终在成员模板上使用“template”前缀,但在非成员模板上使用时它是可选的依赖性表达。
希望有帮助。
Your member function 'foo' needs a return type and you need to use the keyword 'template' when you use member templates in dependent expressions (expressions whose meanings rely directly or indirectly on a generic template parameter)
Perhaps this example will help you appreciate when a member template needs to be prefixed by the 'template' keyword [Note: in the interest of symmetry you may always use the 'template' prefix on member templates, but it is optional when used on a non-dependent expression.
Hope that helps.
在
->
和模板方法名称之间添加关键字template
:Add the keyword
template
between->
and the name of the template method:可能当时还不知道 MessageType。 您是否缺少包含、名称空间解析或声明?
如果不是这样,
getMessageSender
是如何声明的,MessageType
又是如何声明的?一般来说,在 C++ 中,如果此时 T 未知,这不是问题(嗯……虽然很复杂,但仍然如此)。
此外,错误消息通常包含尝试实例化的类型。 尝试至少发布完整的错误消息。
Likely, MessageType isn't known at that point. Are you missing an include, a namespace resolution or a declaration?
if tht's not it, how is
getMessageSender
declared, and how isMessageType
?Generally, in C++ it is not a problem if T is not known at that point (well... it's complicated, but still).
Also, the error message usually contains the type for which it is tried to be insantiated. Try to post the full error message at least.
您是否还有其他类似的对 getMessageSender 等模板化方法的调用?
Do you have other similar calls to methods like getMessageSender that are templatized?
只是缺少函数的返回类型。
t_
成员已完全定义。模板的特化是您针对特定模板参数实现的“特殊”版本。 示例:
std::vector
是通用std::vector
的专用版本。部分特化是通用代码的实现,其中未提供所有模板参数。
It's just the return type of your function that's missing. The
t_
member is fully defined.A specialization of a template is a 'special' version your implement for specific template arguments. An example:
std::vector
is the specialized version of the genericstd::vector
.A partial specialization is an implementation of your generic code where not all template arguments are provided.
这在 Visual Studio 2010 编译器上运行良好。
只是为了保持答案更新!
This works fine on Visual Studio 2010 compiler.
Just to keep the answer updated !!!