1

I just tried the following code

class MailBox{
};

 template<typename T>
     void foo(T){
     cout << "In foo" << endl;
 }

template<typename T>
 void foo1(T){
     foo(T);
 }

 main()
 {
         MailBox m;
         std::vector<MailBox> m1;

         foo1(m1);
 }

We are getting below error while compilation

test1.cpp: In function âvoid foo1(T)â:
test1.cpp:15: error: expected primary-expression before â)â token

Any idea how to resolve this ?

2 Answers 2

7

You're missing formal parameters:

 template<typename T>
     void foo(T t) {
         cout << "In foo" << endl;
 }

 template<typename T>
     void foo1(T t) {
         foo(t);
 }
Sign up to request clarification or add additional context in comments.

2 Comments

to be precise, the formal parameter is only needed in foo1. You might even get "unused parameter" warnings if you provide one in foo.
@Arne: True - I was aiming for simplicity in the answer and also assuming that the real foo would eventually do something useful with its parameter, but you are correct, of course.
1

You're forgetting the names of the parameters!

For instance, foo1() should look like this:

template <typename T>
  foo1 (T myT) {
    foo(myT);
  }

Remember that template'd types are still types, and you need to use them to declare things (like variables) of those types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.