比较简单的日期类的定义与实现,麻烦大家帮忙看看哪里出了问题?

yiruirui0507 2010-10-17 12:36:49

#include<iostream>
#include<iomanip>
using namespace std;
class Date
{
int year,month,day;
public:
void set(int y,int m,int d);
void set(string& s);
bool isLeapYear()const;
void print()const;
};
void Date::set(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
bool Date::isLeapYear()const
{
return (year%4==0 && year%100!=0)||
(year%400 ==0);

}
void Date::print()const
{
cout<<setfill('0');
cout<<setw(4)<<year<<'-'<<setw(2)
<<month<<'-'<<setw(2)<<day<<'\n';
cout<<setfill(' ');

}
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());
}

int main()
{
Date d,e;
d.set(2000,12,6);
e.set("2005-05-05");
e.print();
if(d.isLeapYear())
d.print();
return 0;
}

没用set(string& s)之前还是可以正常编译的,用了这个函数就出错了,麻烦大家给小弟看看,最好能把year=atoi(s.substr(0,4).c_str());给小弟解释一下,3Q了!
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiruirui0507 2010-10-17
  • 打赏
  • 举报
回复
c_str函数的返回值是const char*的,不能直接赋值给char*,所以就需要我们进行相应的操作转化
#include<iostream>
#include<string>
using namespace std;
void main()
{
char *ar="2030";
int a=atoi(ar);
cout<<a<<endl;
string add_to="hello!";
cout<<add_to<<endl;
const string add_on="baby";
const char* cfirst=add_to.c_str();
const char* csecond=add_on.c_str();
char* copy=new char[strlen(cfirst)+strlen(csecond)+1];
strcpy(copy,cfirst);
cout<<copy<<endl;
add_to=copy;
delete [] copy;
cout<<add_to<<endl;
}
yiruirui0507 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gules 的回复:]
set(string& s)函数的参数要用const string&,因为string类的c_str()函数为const。
尽量使用const。
[/Quote]

非常强悍,OK!细节问题!结贴散分走人!
某某9 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gules 的回复:]

set(string& s)函数的参数要用const string&,因为string类的c_str()函数为const。
尽量使用const。
[/Quote]

void set(const string& s);
gules 2010-10-17
  • 打赏
  • 举报
回复
set(string& s)函数的参数要用const string&,因为string类的c_str()函数为const。
尽量使用const。
getline 2010-10-17
  • 打赏
  • 举报
回复
void set(const string& s);
yiruirui0507 2010-10-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 qq120848369 的回复:]
你没#include <string>,发生一切你都没毛病!

atoi: ascii to integer .

s.substr截取string s的0..3字符子串,转化成数字赋值给year.
[/Quote]

错误信息如下:
--------------------Configuration: c - Win32 Debug--------------------
Compiling...
c.cpp
C:\Documents and Settings\Administrator\桌面\c\c.cpp(80) : 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 st
d::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 时出错.

c.exe - 1 error(s), 0 warning(s)
qq120848369 2010-10-17
  • 打赏
  • 举报
回复
你没#include <string>,发生一切你都没毛病!

atoi: ascii to integer .

s.substr截取string s的0..3字符子串,转化成数字赋值给year.

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧