c++ 中的类模板内的模板类

发布于 2024-07-26 21:24:45 字数 870 浏览 4 评论 0原文

这里的菜鸟仍在尝试模板。 尝试写一个消息处理类模板

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

糖粟与秋泊 2024-08-02 21:24:45

您需要使用关键字“template”

t_->template getMessageSender<MessageType>();  // ok
t_->getMessageSender<MessageType>(); // not ok

您的成员函数“foo”需要一个返回类型,并且当您在依赖表达式(其含义直接或间接依赖于通用模板参数的表达式)中使用成员模板时, 成员模板需要以“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

  }

};

希望有帮助。

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)

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.

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.

白衬杉格子梦 2024-08-02 21:24:45

-> 和模板方法名称之间添加关键字 template

t_->template getMessageSender<MessageType>();

Add the keyword template between -> and the name of the template method:

t_->template getMessageSender<MessageType>();
夏末的微笑 2024-08-02 21:24:45

可能当时还不知道 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 is MessageType?

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.

南城追梦 2024-08-02 21:24:45

您是否还有其他类似的对 getMessageSender 等模板化方法的调用?

t_->getMessageSender<MessageType>();

Do you have other similar calls to methods like getMessageSender that are templatized?

t_->getMessageSender<MessageType>();
当梦初醒 2024-08-02 21:24:45

只是缺少函数的返回类型。 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 generic std::vector.

A partial specialization is an implementation of your generic code where not all template arguments are provided.

猫九 2024-08-02 21:24:45

这在 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
    }
}

只是为了保持答案更新!

This works fine on Visual Studio 2010 compiler.

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 !!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文