noob here still experimenting with templates. Trying to write a message processing class template
noob在这里仍在试验模板。试图编写一个消息处理类模板
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.
我添加了必要的头文件来定义MessageType是什么。我之前很久就使用过这个函数,只是不是在这种情况下。
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.
我怀疑编译器不喜欢模板函数在未定义的类模板(unspecialized?)中完全定义(专用?)的事实。我并不完全了解模板“专业化”的原因。大多数解释都集中在“完整”或“部分”的概念上,而不是首先使它成为专业的概念。
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.
如果您想查看更多代码,请道歉。我在工作时没有互联网接入,这就是我正在做的事情,所以我必须将所有内容放入我的心理“暂存器”并将其带回家。
6 个解决方案
#1
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)
你的成员函数'foo'需要一个返回类型,你需要在依赖表达式中使用成员模板时使用关键字'template'(表达式的含义直接或间接依赖于通用模板参数)
t_->template getMessageSender<MessageType>(); // ok
t_->getMessageSender<MessageType>(); // not ok
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.
当成员模板需要以'template'关键字作为前缀时,这个例子可能会帮助你理解[注意:为了对称,你可以在成员模板上使用'template'前缀,但是当它用在一个模板上时它是可选的非依赖性表达。
struct MyType
{
template<class T> void foo() { }
};
template<class U>
struct S
{
template<class T>
void bar()
{
MyType mt; // non-dependent on any template parameter
mt.template foo<int>(); // ok
mt.foo<int>(); // also ok
// 't' is dependent on template parameter T
T t;
t.template foo<int>(); // ok
t.foo<int>(); // not ok
S<T> st; // 'st' is dependent on template parameter T
st.template foo<int>(); // ok
st.foo<int>(); // not ok
S<MyType> s; // non-dependent on any template parameter
s.bar<int>(); // ok
s.template bar<int>(); // also ok
}
};
Hope that helps.
希望有所帮助。
#2
Add the keyword template
between ->
and the name of the template method:
在 - >和模板方法的名称之间添加关键字模板:
t_->template getMessageSender<MessageType>();
#3
Likely, MessageType isn't known at that point. Are you missing an include, a namespace resolution or a declaration?
可能,MessageType在那时还不知道。您是否缺少包含,命名空间解析或声明?
if tht's not it, how is getMessageSender
declared, and how is MessageType
?
如果不是这样,getMessageSender是如何声明的,MessageType是怎样的?
Generally, in C++ it is not a problem if T is not known at that point (well... it's complicated, but still).
一般来说,在C ++中,如果在那时不知道T(这很复杂,但仍然是),这不是问题。
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.
此外,错误消息通常包含尝试实例化的类型。尝试至少发布完整的错误消息。
#4
Do you have other similar calls to methods like getMessageSender that are templatized?
你有其他类似的调用方法,如getMessageSender,这些方法是模板化的吗?
t_->getMessageSender<MessageType>();
#5
It's just the return type of your function that's missing. The t_
member is fully defined.
这只是你失去的函数的返回类型。 t_成员已完全定义。
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 generic std::vector
.
模板的特化是特殊模板参数的“特殊”版本。例如:std :: vector是通用std :: vector的专用版本。
A partial specialization is an implementation of your generic code where not all template arguments are provided.
部分特化是您的通用代码的实现,其中不提供所有模板参数。
#6
This works fine on Visual Studio 2010 compiler.
这适用于Visual Studio 2010编译器。
class One
{
public:
void newFoo() ;
template < class T > void foo()
{
T obj ; // obj is dependent on template parameter
obj.newFoo() ; // and this works
}
}
Just to keep the answer updated !!!
只是为了保持答案更新!
#1
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)
你的成员函数'foo'需要一个返回类型,你需要在依赖表达式中使用成员模板时使用关键字'template'(表达式的含义直接或间接依赖于通用模板参数)
t_->template getMessageSender<MessageType>(); // ok
t_->getMessageSender<MessageType>(); // not ok
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.
当成员模板需要以'template'关键字作为前缀时,这个例子可能会帮助你理解[注意:为了对称,你可以在成员模板上使用'template'前缀,但是当它用在一个模板上时它是可选的非依赖性表达。
struct MyType
{
template<class T> void foo() { }
};
template<class U>
struct S
{
template<class T>
void bar()
{
MyType mt; // non-dependent on any template parameter
mt.template foo<int>(); // ok
mt.foo<int>(); // also ok
// 't' is dependent on template parameter T
T t;
t.template foo<int>(); // ok
t.foo<int>(); // not ok
S<T> st; // 'st' is dependent on template parameter T
st.template foo<int>(); // ok
st.foo<int>(); // not ok
S<MyType> s; // non-dependent on any template parameter
s.bar<int>(); // ok
s.template bar<int>(); // also ok
}
};
Hope that helps.
希望有所帮助。
#2
Add the keyword template
between ->
and the name of the template method:
在 - >和模板方法的名称之间添加关键字模板:
t_->template getMessageSender<MessageType>();
#3
Likely, MessageType isn't known at that point. Are you missing an include, a namespace resolution or a declaration?
可能,MessageType在那时还不知道。您是否缺少包含,命名空间解析或声明?
if tht's not it, how is getMessageSender
declared, and how is MessageType
?
如果不是这样,getMessageSender是如何声明的,MessageType是怎样的?
Generally, in C++ it is not a problem if T is not known at that point (well... it's complicated, but still).
一般来说,在C ++中,如果在那时不知道T(这很复杂,但仍然是),这不是问题。
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.
此外,错误消息通常包含尝试实例化的类型。尝试至少发布完整的错误消息。
#4
Do you have other similar calls to methods like getMessageSender that are templatized?
你有其他类似的调用方法,如getMessageSender,这些方法是模板化的吗?
t_->getMessageSender<MessageType>();
#5
It's just the return type of your function that's missing. The t_
member is fully defined.
这只是你失去的函数的返回类型。 t_成员已完全定义。
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 generic std::vector
.
模板的特化是特殊模板参数的“特殊”版本。例如:std :: vector是通用std :: vector的专用版本。
A partial specialization is an implementation of your generic code where not all template arguments are provided.
部分特化是您的通用代码的实现,其中不提供所有模板参数。
#6
This works fine on Visual Studio 2010 compiler.
这适用于Visual Studio 2010编译器。
class One
{
public:
void newFoo() ;
template < class T > void foo()
{
T obj ; // obj is dependent on template parameter
obj.newFoo() ; // and this works
}
}
Just to keep the answer updated !!!
只是为了保持答案更新!