arguments that are pair<int, int>

Feb 11, 2020 at 5:01pm
Hi, I am trying to create a function that has a parameter that is a pair<int,int> but i cant figure out how to pass the values to it, i seem to be getting errors of "no suitable constructor exists to concert int to std::pair<int,int>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
The code i have is

pair declared

std::pair<int,int>val;

void addVal(val, int addedVal);

void Val::addedVal(val, int addedVal)
{
}

and then I am trying to call the addedVal function using 

addedVal(incomeA, incomeB, valAddedA);

the above line is where I am getting the error, so I must not be passing these values correctly, I am wondering how i pass these.
Feb 11, 2020 at 5:18pm
Not a lot of context to go by, but you probably need to call like this:

addedVal(val, valAddedA);

Also you need to declare your function's parameters like this:

void addVal(std::pair<int, int> val, int addedVal);
Feb 11, 2020 at 7:00pm
Er, you need to supply a whole lot more context.

It looks like you have a “Val” class, holding some pair of income objects with an unknown-to-us relationship? And you wish to ‘add’ some integer value to this pair in some way using a member function? And possibly a free function as well?
Feb 11, 2020 at 7:15pm
ok so what I have is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Val.h

class Val
{
public:
std::pair<int, int>val;

void addVal(val, int addedVal)
}

Val.cpp

void Val::addVal(val, int addedVal)
{

}


but i want to be able to take in 3 values from the user and pass them into addedVal, so I am calling 

addVal(incomeA, incomeB, valAddedA);

and incomeA, B and valAddedA are the values taken in from the user, but i think its a problem because it is two individual ints rather than passing them in as a pair, but I am not sure since i have not done this before. I am not sure if this has explained it better
Feb 11, 2020 at 7:34pm
Still not entirely sure what you're up to, so I can't say that this is the best way, but...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <utility>

class Val
{
public:
    using Pair = std::pair<int, int>;
    Pair val;
    void addVal(Pair v, int addedVal);
};

void Val::addVal(Pair v, int addedVal)
{
    val = v;
    // what is addedVal for?
}

int main()
{
    int incomeA{1}, incomeB{2}, valAddedA{3};
    Val val;
    val.addVal(std::make_pair(incomeA, incomeB), valAddedA);
}


BTW, don't write your question inside the code tags!
Topic archived. No new replies allowed.