C++ :函数中无需改变传入参数的值时,定义时把 “Item & x ” 都改成 \"const Item & x\"
来源:互联网 发布:常用nosql数据库 编辑:程序博客网 时间:2024/06/05 04:56
//f080204.h
#ifndef f080204
#define f080204
#include<iostream>
#include<iomanip>
using namespace std;
class Date {
int year, month, day;
public:
void set(int y, int m, int n);
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(' ');
}
#endif
//f080204.cpp#include"f080204.h"
int main(){
Date d, e;
d.set(2000, 2, 6);
e.set("2005-05-05");
e.print();
if(d.isLeapYear())
d.print();
return(0);
}
错误:
Compiling...
f080204.cpp
F:\C++练习\f080204.cpp(6) : 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_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
执行 cl.exe 时出错.
解决:将蓝色字体分别改为:
void set(const string& s);
void Date::set( const string &s){
- C++ :函数中无需改变传入参数的值时,定义时把 “Item & x ” 都改成 \"const Item & x\"
- WebService中更新函数传入值,再调用其中函数时,函数的参数还是未改变
- 改变navigation drawer中item被选中时的样式
- ListView中删掉某个item时出现整个ListView都看不见,但是数据源有改变
- [C]C语言中函数参数传递的三种方式(x,*x,&x)
- item()和iteritem(),python3.X中items()
- 在函数中改变传入的参数值:引用参数的使用
- [翻译] Effective C++, 3rd Edition, Item 37: 绝不要重定义一个函数的 inherited default parameter value(通过继承得到的缺省参数值)
- android 改变listview的item中view的值
- 改变CListCtrl中Item的图片
- 在函数中没有定义参数时,调用方法传入参数应该怎么办?
- 改变item的颜色
- 解决: 用代码改变listview的item背景色时,点击一个item,每隔几个item背景色重复
- Item 46:需要类型转换时,应当在类模板中定义非成员函数
- Post请求传递参数时,最好在请求头加上item.ContentType = "application/x-www-form-urlencoded";
- 安卓ListView的item中含有EditText,动态添加item时EditText值的保存
- Android中在点击GridView的item时改变其背景
- ListView Item 选中时 改变 TextView 的字体颜色
- MVC的各个部分都有那些技术来实现
- 互联网英语词汇
- SQL2000;安装程序配置服务器失败
- LeetCode - 448. Find All Numbers Disappeared in an Array
- C++的一个错误
- C++ :函数中无需改变传入参数的值时,定义时把 “Item & x ” 都改成 \"const Item & x\"
- c++:error C2593: \'operator <<\' is ambiguous
- error C2512: \'CSample\' : no appropriate default constructor available
- error C2679
- error LNK2001
- Ubuntu安装webpack
- error C2541: delete : cannot delete objects that are not pointers
- C++词汇解析集锦 编程开发人员必备
- c的一些函数(转)