注册 登录
编程论坛 C++教室

[求助]类重载的时候报错!!

zbcdn 发布于 2007-05-14 19:57, 491 次点击
#include "stdafx.h"
#include<iostream>
#include<iomanip>
using namespace std;
//------------------------------------------------
class Date{
public:
int year,month,day;
public:
void set1(int y,int m,int d);
void set1(string& s);
};
//---------------------------------------------------
void Date::set1(int y,int m,int d)
{
year=y; month=m; day=d;
}
//---------------------------------------------------
void Date::set1(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());
}
//---------------------------------------------------
//---------------------------------------------------
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.set1(2000,12,6);
e.set1("2005-05-05");
e.print();
d.print();
}


为什么在定义e.set1("2005-05-05");的时候系统报错啊?请大家帮帮忙.这个是书上的一段例子!
8 回复
#2
leeco2007-05-14 21:52
强制转换了一下就可以了

#include<iostream>
#include<iomanip>
using namespace std;
//------------------------------------------------
class Date{
public:
    int year,month,day;
public:
    void set1(int y,int m,int d);
    void set1(string& s);
    void print();
};
//---------------------------------------------------
void Date::set1(int y,int m,int d)
{
    year=y; month=m; day=d;
}
//---------------------------------------------------
void Date::set1(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());
}
//---------------------------------------------------
//---------------------------------------------------
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.set1(2000,12,6);
    e.set1(string(\"2005-05-05\"));
    e.print();
        d.print();
}
#3
zbcdn2007-05-14 22:18
回复:(leeco)强制转换了一下就可以了[code]#includ...

谢谢!~~

#4
raulxxyuer2007-05-14 22:40
写到了
#5
aipb20072007-05-14 22:49

这是强制转换吗?

#6
raulxxyuer2007-05-14 23:28
啊,那那是什么呢?
#7
aipb20072007-05-15 11:47
以下是引用raulxxyuer在2007-5-14 23:28:00的发言:
啊,那那是什么呢?

创建一个临时的string对象。

#8
PcrazyC2007-05-15 12:06
#include "stdafx.h"

这是什么头文件,谁能说一下
#9
aipb20072007-05-15 13:01
编译器通过stdafx.h来使用预编译头文件

MFC中用的比较多,具体不很了解!
1