Class template inside a class in C ++

noob is still experimenting with templates here. 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_

};

Everything is defined in the header file. I built and tested my class, and everything is fine. Now I am 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 the error of an incorrect type expression before the '>' token.

I added the necessary header files to determine what MessageType is. I have used this feature many times before, just not in this context.

, , (?) undefined (?). , "". "" "", , .

, . , , , "" .

+5
6

- 'foo' , 'template', - (, )

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

, , - "" [: "" -, , .

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

  }

};

, .

+9

template -> :

t_->template getMessageSender<MessageType>();
+2

, MessageType . include, ?

tht , getMessageSender, MessageType?

, ++ , T (... , ).

, , . .

0

, getMessageSender, templatized?

t_->getMessageSender<MessageType>();
0

, . t_ .

- "" . : std::vector - std::vector.

Partial specialization is the implementation of your shared code, where not all template arguments are provided.

0
source

This works fine in the 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 update the answer!

0
source

All Articles