1

Environment : Visual Studio 2008 Professional Edition

I am trying to debug Hexadecimal to decimal conversion but unfortunately getting "term does not evaluate to a function taking 3 arguments" this error.Can anyone suggest how to solve this issue ?

code:

#include <string>
using namespace std;

int main()
{
    int stoi;
    int number = 0;

    string hex_string = "12345";
    number = stoi(hex_string, 0, 16);
    cout << "hex_string: " << hex_string << endl;
    cout << "number: " << number << endl;

    return 0;
}
  • 2
    `stoi` is an integer, why are you trying to call it as a function? – user7860670 Oct 23 '19 at 07:03
  • @VTT if it is not declare it gives an error like ""error C3861: 'stoi': identifier not found" –  Oct 23 '19 at 07:05
  • You want to use the std::stoi https://en.cppreference.com/w/cpp/string/basic_string/stol (another reason why using namespace std is wrong) –  Oct 23 '19 at 07:07
  • @Adderbill no it doesn't - https://wandbox.org/permlink/p0BmWNaYTAaFm8HR – Ayjay Oct 23 '19 at 07:07
  • The support for C++11 (where `stoi` was introduced) is limited at best. You need a different solution or a different compiler. (Note that if this is a school exercise, you're supposed to do the conversion yourself, not use a library.) – molbdnilo Oct 23 '19 at 07:26

3 Answers3

2

This is why you shouldn't do using namespace std;. Get rid of it and fix the program by putting std:: before everything in the std namespace.

#include <iostream>
#include <string>

int main()
{
    int stoi;
    int number = 0;

    std::string hex_string = "12345";
    number = std::stoi(hex_string, 0, 16);
    std::cout << "hex_string: " << hex_string << std::endl;
    std::cout << "number: " << number << std::endl;

    return 0;
}

You could also just rename your stoi integer to not clash with std::stoi, but it is strongly recommended to not have using namespace std; in your code.

If you can't use stoi at all because Visual Studio 2008 doesn't support C++11 and you can't upgrade to a newer version, see here for alternatives. But in the long way it would probably be a better idea to install a newer IDE if possible.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Now the error like 1]error C2039: 'stoi' : is not a member of 'std' 2]error C2064: term does not evaluate to a function taking 3 arguments –  Oct 23 '19 at 07:09
  • What's your version? You need at least C++11 to use `stoi`. – Blaze Oct 23 '19 at 07:11
  • You should use C++11 or later –  Oct 23 '19 at 07:11
  • @Blaze I have Microsoft Visual C++ 10.0.40219 –  Oct 23 '19 at 07:23
  • @Adderbill in that case you can't use `std::stoi` and will have to use an alternative. See [here](https://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c) for some suggestions, especially [this](https://stackoverflow.com/a/6154614/10411602) answer. – Blaze Oct 23 '19 at 07:27
1

Because stoi is a function from string library you dont redefine stoi as int stoi. Delete int stoi it would be succeeded.

Complete code like these

#include <string>
#include <iostream>
using namespace std;

int main()
{

    int number = 0;

    string hex_string = "12345";
    number = stoi(hex_string, nullptr, 16);
    cout << "hex_string: " << hex_string << endl;
    cout << "number: " << number << endl;

    return 0;
}
Serhan Erkovan
  • 481
  • 4
  • 18
  • 1] error C2039: 'stoi' : is not a member of 'std' and 2] error C3861: 'stoi': identifier not found, this are the errors if i removed int stoi –  Oct 23 '19 at 07:11
  • @Adderbill can you editted your publishment with our suggestion, after this we could help you – Serhan Erkovan Oct 23 '19 at 07:14
0

Thanks you everyone for your response ! final code which is debug successfully on visual studio 2008 for conversion of hexadecimal to decimal

#include <iostream>
using namespace std ;
#include <sstream>

int wmain() {
int binNumber ;
unsigned int decimal;
string hexString = "0x3d"; //you may or may not add 0x before
stringstream myStream;
myStream <<hex <<hexString;
myStream >>binNumber;
cout <<binNumber <<decimal;
return 0;
}