I thought I was doing this right but it doesn’t seem to be working. I’m basically experimenting with a queue and it works fine with one data type but now I’m trying to add multiple(in the end I want to have a int and a list of ints).
Here’s the code:
#include <iostream>
#include <queue>
using namespace std;
int main()
{
struct product {
int x;
int y;
} ;
queue<product> q;
q.push(100, 100);
q.push(200, 100);
q.push(300, 100);
q.push(400, 100);
cout << "Size of the queue: " << q.size() << endl;
while (!q.empty()) {
cout << q.front() << endl;
q.pop();
}
}
It works without the struct, but obviously it only accepts one variable for each item in the queue that way. Is there a way to have multiple items?
I think the type specified for the template type cannot be a local definition. Change to:
As others have already stated, add a constructor to
productthat accepts both arguments:You could also overload
operator<<for outputting aproduct: