Home > Blockchain >  How can I fix the missing template arguments before '(' token problem here?
How can I fix the missing template arguments before '(' token problem here?

Time:10-22

I am getting the problem on line 15 and 20 where i am trying to pushback elements in to the pair vector

#include <iostream>
#include <vector>
#include <utility>

using namespace std;


int main()
{
    int x, y, a, b, c, d, j, m, v;
    vector<pair<int, int> > ladder;
    cin >> x >> y;
    for (int i = 0; i < x; i  ) {
        cin >> a >> b;
        ladder.push_back(pair(a, b));
    }
    vector<pair<int, int> > snake;
    for (int i = 0; i < y; i  ) {
        cin >> c >> d;
        snake.push_back(pair(c, d));
    }
    vector<int> moves;
    cin >> v;
    while (v != 0) {
        moves.push_back(v);
        v = 0;
        cin >> v;
    }
    return 0;
}

My errors are:

prog.cpp: In function ‘int main()’:
prog.cpp:15:30: error: missing template arguments before ‘(’ token
         ladder.push_back(pair(a, b));
                              ^
prog.cpp:20:29: error: missing template arguments before ‘(’ token
         snake.push_back(pair(c, d));

I have the code here to test: https://ideone.com/ZPKP4s

CodePudding user response:

Your issue is on these two lines:

ladder.push_back(pair(a, b));
ladder.push_back(pair(c, d));

You need to specify what types of pairs these are:

ladder.push_back(pair<int, int>(a, b));
ladder.push_back(pair<int, int>(c, d));

CodePudding user response:

The template parameter deduction for std::pair does not work until C 17.

You need to explicitly specify the template parameters, std::pair<int,int>(a, b), or by-pass that completely and construct the pair in-place by using the vector emplace_back member function which forwards the given arguments to the pair<int,int> constructor in the vector as-is:

ladder.emplace_back(a, b);
// ...
snake.emplace_back(c, d);

Here's your code using emplace_back instead.

CodePudding user response:

ladder.push_back(pair(a, b));

You should pass template argument of std::pair class

ladder.push_back(pair<int, int>(a, b));
  • Related