C++ :函数中无需改变传入参数的值时,定义时把 “Item & x ” 都改成 \"const Item & x\"
2017-01-17 21:25
585 查看
//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){
#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++函数参数里的const, 处理 error : passing const as "this"
- C++的基础知识(十一)--数组做函数参数、数组名a与&a区别、数组名a的"数据类型"
- haXe中使用callback关键字"特化"一个函数从而改变其参数列表
- 读书笔记 effective c++ Item 37 永远不要重新定义继承而来的函数默认参数值
- 【转】深入理解C++的动态绑定和静态绑定 & 不要重定义虚函数中的默认参数
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数 .txt
- C++关于参数为const引用类型的函数调用参数成员函数的一个问题
- 牛腩新闻发布系统 (3) 存过过程或函数""需要""参数,但未提供该参数
- cvLoadImage 不能将参数1从 "_TCHAR*" 转换为"const char *"
- javascript入门系列演示·函数的定义以及简单参数使用,调用函数
- 解决C++ 调用 C 函数时unresolved external symbol "int __cdecl addInC1(void)" (?addInC1@@YAHXZ)之类的错误
- C++程序设计实验报告(二十)---实现冒泡排序算法,并将之定义为一个函数,其中参数是指向数组的指针变量
- [收藏]C++ Tips(9)--函数参数中的const
- 为什么在C++里调用被C编译器编译的函数,要加extern "c"
- C++.net 定义类 将类对象作为函数参数传入另一个类 出现错误
- C#: 调用存储过程或函数显示"需要"参数,但未提供该参数的解决方法
- c/c++笔试题——const类型的成员函数内部如何改变成员变量
- C && C++函数定义小结
- C/C++ 变长参数 (自己定义类似printf的函数)
- [收藏]C++ Tips(9)--函数参数中的const