转换时未在此作用域中声明Strtoull

strtoull was not declared in this scope while converting?

本文关键字:声明 Strtoull 作用域 转换      更新时间:2023-10-16

我在eclipse CDT中使用c++,我试图通过使用strtoull将字符串转换为uint64_t,但每次我得到下面的错误信息-

..srcHelloTest.cpp:39:42: error: strtoull was not declared in this scope

下面是我的c++示例

#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;
}

我做错了什么吗?

为什么你的解决方案不起作用,别人已经指出了。但目前还没有一个好的替代方案。

试试c++ 03 strtoull的用法:

#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 );
}

或者,从c++ 11开始,直接从std::string通过stoull(这只是上面的包装器,但在代码中节省了一个包含和一个函数调用):

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

永远不要使用char[]或指针,如果你有一个可行的选择。它们是c++的阴暗面。更快,更容易,更诱人。一旦你走上黑暗之路,它将永远主宰你的命运,吞噬你。: -)

strtoull(const char *, char * *, int)你给了它一个std::字符串,正如@juanchopanza

指出的那样

我想到的解决方案是

#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;
}
我得到的输出是:1234567直接从eclipse控制台。

同样在你的程序结束时,你有返回0超出作用域和一个额外的大括号。