-2

我在 eclipse CDT 中使用 C++,我试图通过使用将字符串转换为 uint64_tstrtoull但每次我收到以下错误消息 -

..\src\HelloTest.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;
}

我做错了什么吗?

4

2 回答 2

1

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

尝试使用 C++03strtoull代替:

#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::stringvia执行stoull(这只是上述内容的包装,但在代码中节省了一个包含和一个函数调用

#include <string>

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

char[]如果您有可行的替代方案,切勿使用或指针。他们是 C++ 的阴暗面。更快,更容易,更诱人。如果一旦你开始走上黑暗的道路,它将永远主宰你的命运,它会吞噬你。;-)

于 2014-06-10T20:47:55.493 回答
0

strtoull 的结构是: strtoull(const char *, char * *, int) 正如@juanchopanza 所指出的,你给了它一个 std::string

这是我想出的解决方案是

#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 超出范围。

于 2014-06-10T20:31:45.833 回答