主题:关于string 和 字符串 的问题?
liuyu1768 [专家分:30] 发布于 2009-10-28 14:58:00
//===========================
//f0804.cpp
//重载成员函数
//===========================
#include<iostream>
#include<iomanip>
//#include<string>
using namespace std;
//---------------------------
class Date
{
int year,month,day;
public:
void set(int,int,int);
void set(string& s);
bool isLeapYear();
void print();
};//------------------------------
void Date::set(int y, int m, int d)
{
year=y;
month=m;
day=d;
}//-------------------------------
void Date::set(string& s)
{
year=atoi(s.substr(0,4).c_str());
month=atoi(s.substr(5,2).c_str());
day=atoi(s.substr(8,2).c_str());
}//-------------------------------
bool Date::isLeapYear()
{
return (year%4==0 && year%100!=0) || (year%400==0);
}//---------------------------------
void Date::print()
{
cout<<setfill('0');
cout<<setw(4)<<year<<'-'<<setw(2)<<month<<'-'<<setw(2)<<day<<'\n';
cout<<setfill(' ');
}//-----------------------------------
int main()
{
Date d,e;
d.set(2000,12,6);
e.set("2005-05-05");
e.print();
if(d.isLeapYear())
d.print();
return 0;
}//====================================
这个程序运行之后 出错:
error C2664: 'void __thiscall Date::set(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' : cannot convert parameter 1 from 'char [11]' to 'class std::basic_st
ring<char,struct std::char_traits<char>,class std::allocator<char> > &'
A reference that is not to 'const' cannot be bound to a non-lvalue
这是为什么啊?定义的时候是string啊。。。
回复列表 (共10个回复)
沙发
cgl_lgs [专家分:21040] 发布于 2009-10-28 15:16:00
把
string&
改为
const string &
板凳
liuyu1768 [专家分:30] 发布于 2009-10-28 15:27:00
[quote]把
string&
改为
const string &[/quote]
真的可以诶,不过这是为什么呢?
3 楼
cgl_lgs [专家分:21040] 发布于 2009-10-28 22:39:00
因为你传入的数据本来就是const char *,所以只能初始化const string&
4 楼
liuyu1768 [专家分:30] 发布于 2009-10-29 18:26:00
[quote]因为你传入的数据本来就是const char *,所以只能初始化const string&[/quote]
哦哦,也就是const定义的之后就不能用一般变量了吧, 谢啦啊。
5 楼
eastcowboy [专家分:25370] 发布于 2009-10-29 23:22:00
楼主理解偏差了。
e.set("2005-05-05");
这里由于set函数所需要的是string&类型的参数,而你传递的是"2005-05-05",类型不匹配。因此,编译器会先自动创建一个临时的string类型变量(实际上会把"2005-05-05"作为参数,调用string的构造函数),然后把这个临时的string类型变量作为参数去调用e.set,最后再销毁这个临时变量。
在C++的标准语法中,临时变量是不能得到它的“非const引用”的,我们只能得到它的“const引用”。因此,写string&编译出错,而写const string&则编译通过。
6 楼
sharp19 [专家分:1410] 发布于 2009-10-30 19:48:00
真有点复杂,哎~~~
7 楼
liuyu1768 [专家分:30] 发布于 2009-10-30 22:22:00
[quote]楼主理解偏差了。
e.set("2005-05-05");
这里由于set函数所需要的是string&类型的参数,而你传递的是"2005-05-05",类型不匹配。因此,编译器会先自动创建一个临时的string类型变量(实际上会把"2005-05-05"作为参数,调用string的构造函数),然后把这个临时的string类型变量作为参数去调用e.set,最后再销毁这个临时变量。
在C++的标准语法中,临时变量是不能得到它的“非const引用”的,我们只能得到它的“const引用”。因此,写string&编译出错,而写const string&则编译通过。[/quote]
那请问一下"2005-05-05"是属于c-串,就不是string类的了么?那以后只要是要之直接用字符串就都用const引用了吧?
8 楼
eastcowboy [专家分:25370] 发布于 2009-10-31 15:56:00
"2005-05-05"的类型其实是char[11]。它可以隐式的转化为const char*。而const char*可以作为参数,调用string的构造函数,从而得到一个string。最后这个string就可以用来调用e.set函数了。
一般来说,“const string&”是一个习惯写法。在把一个string值作为参数传递到一个函数内的时候,几乎总是会这样写。
9 楼
liuyu1768 [专家分:30] 发布于 2009-11-03 20:33:00
[quote]"2005-05-05"的类型其实是char[11]。它可以隐式的转化为const char*。而const char*可以作为参数,调用string的构造函数,从而得到一个string。最后这个string就可以用来调用e.set函数了。
一般来说,“const string&”是一个习惯写法。在把一个string值作为参数传递到一个函数内的时候,几乎总是会这样写。[/quote]
不算太理解吧,不过就是以后这种情况就都用“const string&”吧。。。以后再回过头来看这个问题应该可以理解吧?
10 楼
liuyu1768 [专家分:30] 发布于 2009-11-03 20:49:00
[quote]"2005-05-05"的类型其实是char[11]。它可以隐式的转化为const char*。而const char*可以作为参数,调用string的构造函数,从而得到一个string。最后这个string就可以用来调用e.set函数了。
一般来说,“const string&”是一个习惯写法。在把一个string值作为参数传递到一个函数内的时候,几乎总是会这样写。[/quote]
好像有点理解了,是说string在STL里面定义的时候用const char*定义的?然后用const char*作为参数的时候也可以转换为string?但是临时变量要用const定义,所以用“const string&”
我来回复