-1

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

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 1
    Line 15 is `vector moves;` and line 20 is `cin >> v;` - if I count correctly - better put a comment behind the relevant lines and please post the exact error message as text. – Lukas-T Oct 21 '21 at 12:58
  • Also, which compiler you are using and what command line options you are using, particularly, which language standard (CTAD is new for C++17). – Raymond Chen Oct 21 '21 at 12:59
  • 1
    Use a C++17 compiler. – n. m. could be an AI Oct 21 '21 at 13:01
  • @RaymondChen I am using c++14 so maybe that could be an issue. But i fixed the problem using scohoe001s method in the comment. Maybe older version needs specifying whereas newer versions dont –  Oct 21 '21 at 13:15
  • C++14 requires you to specify the type. C++17 adds a feature called CTAD which lets you omit it. C++14 does have a simplified pair-construction mechanism: `ladder.push_back(std::make_pair(a, b))`. Or you can use `ladder.emplace_back(a, b)` and let `emplace` do the pair-making. – Raymond Chen Oct 21 '21 at 13:21

3 Answers3

3

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.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
1

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));
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • Since this is constructing a `pair` with `a` and `b` and then pushing it onto the vector, it would even be better to just use `ladder.emplace_back(a, b)` here. See also https://en.cppreference.com/w/cpp/container/vector/emplace_back – Louis Cloete Oct 21 '21 at 13:14
-1
ladder.push_back(pair(a, b));

You should pass template argument of std::pair class

ladder.push_back(pair<int, int>(a, b));
SungJinKang
  • 409
  • 3
  • 9