关于抽象类new的问题

qq_41780568 2018-03-08 11:13:48

//=====================
//idate.h
//=====================
#ifndef IDATE_H_INCLUDED
#define IDATE_H_INCLUDED
#include <iostream>
using namespace std;
class IDate
{
protected:
virtual int ymd2i() = 0 ;
public:
virtual ~IDate(){}
virtual IDate& operator+(int n) = 0 ;
int operator-( IDate& d)
{
return ymd2i()-d.ymd2i();
}
virtual IDate& operator+=(int n) = 0;
virtual IDate& operator++() = 0;
virtual void print(ostream& o)const = 0 ;
};
IDate& createDate(int y,int m,int d);
IDate& createDate(int n);
IDate& createDate(const string s);
inline ostream& operator<<(ostream& o,const IDate& d)
{
d.print(o);
return o;
}
#endif // IDATE_H_INCLUDED


//====================
//date.h
//====================
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
#include "idate.h"
#include <iostream>
#include <iomanip>
using namespace std;
class Date : public IDate
{
int year;
int month;
int day;
protected:
int ymd2i()const ;
void i2ymd(int n);
static const int tians[];
bool isLeapyear()const
{
return !(year%4) && (year%100) || !(year%400);
}
public:
Date(const string s);
Date(int n)
{
i2ymd(n);
}
Date(int y,int m,int d):year(y),month(m),day(d){}
~Date(){}
void print(ostream& o)const;
Date& operator + (int n)
{
return *new Date(ymd2i()+n);
}
Date& operator+=(int n)
{
i2ymd(ymd2i()+n);
return *this;
}
Date& operator++()
{
return *this += 1;
}
};


#endif // DATE_H_INCLUDED


//===============
//date.cpp
//===============
#include "date.h"
#include <stdlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
const int Date::tians[]= {0,31,59,120,150,181,212,242,273,303,334};
Date::Date(const 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 ::i2ymd(int absDay)
{
absDay = absDay > 0 && absDay < 3650000 ? absDay : 1;
int n = absDay;
for(year = 1;n>isLeapyear()+365;n-=isLeapyear()+365,year++);
for(month = 1;(month < 13 && n>(isLeapyear()&&month > 2))+tians[month];month++)
day = n - (isLeapyear() && month > 2) - tians[month - 1];
}
int Date::ymd2i()const
{
int absDay = (year-1)*365 + (year - 1)/4 - (year-1)/100 + (year-1)/400;
return absDay += tians[month - 1] + (isLeapyear() && month > 2) + day;
}
void Date::print(ostream& o)const
{
o << setfill('0') << setw(4) << year << "-"
<< setw(2) << month << '-' << setw(2) << day << "\n" << setfill(' ');
}
IDate& createDate(int y,int m,int d)
{
return *new Date(y,m,d);
}
IDate& createDate(int n)
{
return *new Date(n);
}
IDate& createDate(const string s)
{
return *new Date(s);
}


//======================
//main.cpp
//======================
#include "date.h"
#include <iostream>
void fn(IDate& d1,IDate& d2)
{
std::cout << d2 - d1 << endl;
std::cout << ++d;
}
int main()
{
IDate& rd1 = createDate(2005,1,6);
IDate& rd2 = createDate(2005,2,3);
fn(rd1,rd2);
delete &rd1;
delete &rd2;
}

源代码如上,编译时四个出现new的地方都报错了:error: invalid new-expression of abstract class type ‘Date’
就是说抽象类不允许new表达式,网上说我有纯虚函数没有实现,我不太明白,idate.h里面五个纯虚函数都实现了啊,请问是哪里的问题。|
...全文
376 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
mstlq 2018-03-08
  • 打赏
  • 举报
回复
看漏cv限定符号,3楼说的对
wallesyoyo 2018-03-08
  • 打赏
  • 举报
回复
你的IDate里面的纯虚函数ymd2i,是非const类型的,而你Date里面的实现版本是const类型的。所以你没有实现IDate里面给出的非const版本的纯虚函数ymd2i,你的Date还是被认为是抽象类。 把你IDate里面的ymd2i加上const。
mstlq 2018-03-08
  • 打赏
  • 举报
回复
楼主编码器太古老?
Date& operator + (int n)
     {
          return *new Date(ymd2i()+n);
     }
     Date& operator+=(int n)
     {
          i2ymd(ymd2i()+n);
          return *this;
     }
     Date& operator++()
     {
          return *this += 1;
     }
改成
IDate& operator + (int n)
     {
          return *new Date(ymd2i()+n);
     }
     IDate& operator+=(int n)
     {
          i2ymd(ymd2i()+n);
          return *this;
     }
     IDate& operator++()
     {
          return *this += 1;
     }
试试?
paschen 版主 2018-03-08
  • 打赏
  • 举报
回复
抽象类(即带有纯虚函数的类)不能创建类对象,纯虚函数比如你上面的: virtual IDate& operator+(int n) = 0 ;

64,642

社区成员

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

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