返回信息流#include<iostream>
#include<string>
using namespace std;
class sale{
public:
sale(const string &s):isbn(s),unit(0){}//[color=#FF00FF]为什么这里必须在string引用前加const[/color]private:
string isbn;
int unit;
};
void main()
{
sale enlishbook("999999");
//
//
}
就是类的构造函数的知识,不加const,编译器编译通不过,提示错误:
error C2664: '__thiscall sale::sale(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' : cannot convert parameter 1 from 'char [7]' to 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & A reference that is not to 'const' cannot be bound to a non-lvalue
这是一条镜像帖。来源:北邮人论坛 / cpp / #32539同步于 2009/12/2
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
初学c++,求牛人解惑,不胜感激...
walker09
2009/12/2镜像同步28 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
cout<<0是啥……
初学就上类么……
你那个//为什么
我猜是拷贝构造函数?
菜鸟一只
【 在 walker09 (walker) 的大作中提到: 】
: #include<iostream>
: #include<string>
: using namespace std;
: ...................
参数是不需要被修改的,const防止对参数的错误修改
不加也可以,不过最好加上
【 在 walker09 (walker) 的大作中提到: 】
: #include<iostream>
: #include<string>
: using namespace std;
: ...................
晕……问的是const……
【 在 DarkIce (中国承诺不首先使用城管和发改委) 的大作中提到: 】
: 参数是不需要被修改的,const防止对参数的错误修改
: 不加也可以,不过最好加上
【 在 DarkIce 的大作中提到: 】
: 参数是不需要被修改的,const防止对参数的错误修改
: 不加也可以,不过最好加上
我不加的时候就是编译不过,你可以copy到vc上试下啊,谢谢啊。
vs2005编译通过
你的那个错误好像是说不能传递非const值给非左值
不知道你用的是什么编译环境
【 在 walker09 (walker) 的大作中提到: 】
: 我不加的时候就是编译不过,你可以copy到vc上试下啊,谢谢啊。
另外,如果是vc6.0建议卸载后使用vs2005 vs2008
【 在 DarkIce (中国承诺不首先使用城管和发改委) 的大作中提到: 】
: vs2005编译通过
: 你的那个错误好像是说不能传递非const值给非左值
: 不知道你用的是什么编译环境
: ...................
【 在 DarkIce 的大作中提到: 】
: vs2005编译通过
: 你的那个错误好像是说不能传递非const值给非左值
: 不知道你用的是什么编译环境
我用的vc6.0的编译环境啊,如果真的是编译环境的话,去哪里找vc2005,vc2008啊?谢谢