Quy Trinh
Quy Trinh

Reputation: 11

How to handle nested type template in C++

Can anyone help me with this errors. When i compile this simple program

#include<queue> 
using namespace std;
template<typename Queue>
int qtest(Queue & queue,typename Queue::Type item)
 {
    return 0;
 }

int main()
{
    std::queue<int> q;
    int t = qtest(q,3);
}

I get the errors like below

In function 'int main()':
error: no matching function for call to 'qtest(std::queue<int>&, int)'
note: candidate is:
note: template<class Queue> int qtest(Queue&, typename Queue::Type)
note:   template argument deduction/substitution failed:
 In substitution of 'template<class Queue> int qtest(Queue&, typename Queue::Type) [with 
Queue = std::queue<int>]':
   required from here
error: no type named 'Type' in 'class std::queue<int>'
warning: unused variable 't' [-Wunused-variable]

Upvotes: 1

Views: 121

Answers (1)

D-RAJ
D-RAJ

Reputation: 3380

std::queue doesn't have a member type called Type. That's what the compiler is telling us. I'm guessing what you're looking for is std::queue<int>::value_type.

template<typename Queue>
int qtest(Queue & queue,typename Queue::value_type item)
{
    return 0;
}

Reference: cppreference

Upvotes: 3

Related Questions