C2039字符串成员函数.pop_back()和.back()出错
C2039 error for string member function .pop_back() and .back()
我使用和来编程两个在整数和字符串之间交换的函数。第一个函数,字符串intToStr(int x),使用:
1) std::basic_string::push_back
它工作得很好。
但是,当第二个函数int str2Int(const string&str)使用以下成员函数时
1) std::basic_string::pop_back
2) std::basic_string::back
我得到了以下错误:
1) error C2039: 'back' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
2) error C2039: 'pop_back' : is not a member of 'std::basic_string<_Elem,_Traits,_Ax>'
完整的代码如下:
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string intToStr(int x)
{
bool isNegative;
int cnt = 0;
if(x<0)
{
isNegative = true;
x = -x;
}
else
{
isNegative = false;
}
string s;
while(x)
{
s.push_back('0'+x%10);
x /= 10;
cnt ++;
if(cnt%3==0 & x!=0)
s.push_back(',');
}
if(isNegative)
s.push_back('-');
reverse(s.begin(),s.end()); //#include <algorithm>
return s;
}
int str2Int(const string &str)
{
int result=0, isNegative=0;
char temp;
string tempStr = str;
reverse(tempStr.begin(),tempStr.end());
// the following code snippet doesn't work??
// pop_back() and back() are not member function??
while(!tempStr.empty())
{
temp = tempStr.back();
tempStr.pop_back();
if(temp==',')
continue;
else if(temp=='-')
isNegative = 1;
else
result = result*10 + (temp-'0');
}
return isNegative? -result:result;
}
这些成员函数仅存在于C++11中。您必须将代码编译为C++11代码才能正确编译。
Visual Studio 2008附带的编译器不支持C++11。您需要使用更新的编译器。
您可以使用Clang、GCC或升级到Visual Studio 2012。
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- 导入/导出变量时出错
- C2039字符串成员函数.pop_back()和.back()出错