Problem while using stoi fuction.

When I run the following program it shows an error error: call of overloaded ‘stoi(int&)’ is ambiguous

Why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    int str;

    cin>>str;

    //String to int
    int i=stoi(str);

    cout<<i/2<<endl;

    return 0;
}
Last edited on
str in your code is an integer instead of a std::string.

Also, streams convert between numeric and string types for you.
The stoi function takes a string as its first parameter; you're passing an int as its first parameter. There is therefore no overload of the stoi function that exists which takes an int as its first parameter.

In short, you're converting an int to an int which is redundant, so there is no actual function for it.
Topic archived. No new replies allowed.