转换时未在此作用域中声明Strtoull
strtoull was not declared in this scope while converting?
我在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超出作用域和一个额外的大括号。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 转换时未在此作用域中声明Strtoull