-2

I am working with C++ in eclipse CDT and I am trying to convert string to uint64_t by using strtoull but everytime I get below error message -

..\src\HelloTest.cpp:39:42: error: strtoull was not declared in this scope

Below is my C++ example

#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int main() {

    string str = "1234567";
    uint64_t hashing = strtoull(str, 0, 0);
    cout << hashing  << endl;
}

return 0;
}

Is there anything wrong I am doing?

john
  • 11,311
  • 40
  • 131
  • 251

2 Answers2

1

Why your solution doesn't work has already been pointed out by others. But there hasn't been a good alternative suggested yet.

Try this for C++03 strtoull usage instead:

#include <string>
#include <cstdlib>

int main()
{
    std::string str = "1234";
    // Using NULL for second parameter makes the call easier,
    // but reduces your chances to recover from error. Check
    // the docs for details.
    unsigned long long ul = std::strtoull( str.c_str(), NULL, 0 );
}

Or, since C++11, do it directly from std::string via stoull (which is just a wrapper for the above, but saves on one include and one function call in your code):

#include <string>

int main()
{
    std::string str = "1234";
    // See comment above.
    unsigned long long ul = std::stoull( str, nullptr, 0 );
}

Never use char[] or pointers if you have a working alternative. The dark side of C++, they are. Quicker, easier, more seductive. If once you start down the dark path, forever will it dominate your destiny, consume you it will. ;-)

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

the structure for strtoull is: strtoull(const char *, char * *, int) You have given it a std::string as pointed out by @juanchopanza

This is the solution I came up with is

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>

using namespace std;

int main() {

    char str[] = "1234567";

    unsigned long long ul;
    char* new_pos;

    charDoublePointer = 0;
    ul = strtoull(str, &new_pos, 0);

    cout << ul << endl;

    return 0;
}

The output I got was: 1234567 Straight from the eclipse console.

Also at the end of your program you have return 0 out of scope with an extra curly brace.

Puddinglord
  • 133
  • 2
  • 12
  • 2
    Why do you need `char** charDoublePointer = new char*`?. It causes a memory leak. Use `char* new_pos;` and then call `strtoull(str, &new_pos, 0)` –  Jun 10 '14 at 20:37
  • @Tibor Thank you I completely forgot about that. – Puddinglord Jun 10 '14 at 20:41