标题:大家好,我是论坛新人,也是c++新人~带着问题来的,希望大家帮忙看一下啊谢 ...
只看楼主
jojo59516
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-4-2
结帖率:25%
 问题点数:0 回复次数:4 
大家好,我是论坛新人,也是c++新人~带着问题来的,希望大家帮忙看一下啊谢谢~
#include <iostream>
using namespace std;
int l(int y)
{    if (y%400==0)
        return 1;
    else
        if (y%4==0)
            return 1;
        else
            return 0;
}

class Date
{
public:
    Date(int yy,int mm,int dd){y=yy;m=mm;d=dd;}
    Date(Date &p){y=p.y;m=p.m;d=p.d;}
    int gety(){return y;}
    int getm(){return m;}
    int getd(){return d;}
private:
    int y,m,d;
};

class Month
{
public:
    Month(int mm,int ll);
    Month(Month &p){mon=p.mon;l=p.l;}
    int getm(){return mon;}
private:
    int mon,l;
};
Month::Month(int mm,int ll)
{    l=ll;
    switch(mm)
    {    case'1':
        case'3':
        case'5':
        case'7':
        case'8':
        case'10':
        case'12':
            mon=31;
        break;
        case'4':
        case'6':
        case'9':
        case'11':
            mon=30;
        break;
        case'2':
            mon=28+l;
        break;
    }
}

class Year
{
public:
    Year(Date dd);
    Year(Year &p);
    int gety(){return nday;}
private:
    int nday;
    Date d;
};
Year::Year(Date dd):d(dd)
{    Month m(d.getm(),l(d.gety()));
    int i;
    for(i=1,nday=0;i<d.getm();i++)
    {    m(i,l(d.gety()));
        nday+=m.getm();
    }
    nday+=d.getd();
}
Year::Year(Year &p):d(p.d){nday=p.nday;}

int work(Date d1(0,0,0),Date d2(0,0,0))
{    Year y1(d1),y2(d2);
    int i,n=0;
    for (i=d1.gety();i<d2.gety();i++)
        n+=365+l(i);
    n=n+y1.gety()-y2.gety();
    return n;
}

void main()
{    Date date1,date2;
    int yy,mm,dd;
    cout<<"Please enter the beginning date:(yyyymmdd)"<<endl;
    cin>>yy>>mm>>dd;
    date1(yy,mm,dd);
    cout<<"Please enter the endding date:(yyyymmdd)"<<endl;
    cin>>yy>>mm>>dd;
    date2(yy,mm,dd);
    cout<<"There is "<<work(date1,date2)<<" days between "<<date1<<" and "<<date2<<endl;
}
--------------------Configuration: Year - Win32 Debug--------------------
Compiling...
Year.cpp
F:\study\VC++\Year\Year.cpp(72) : error C2064: term does not evaluate to a function
F:\study\VC++\Year\Year.cpp(79) : error C2059: syntax error : 'constant'
F:\study\VC++\Year\Year.cpp(79) : error C2059: syntax error : 'constant'
F:\study\VC++\Year\Year.cpp(80) : error C2664: '__thiscall Year::Year(class Date)' : cannot convert parameter 1 from 'class Date (__cdecl *)(void)' to 'class Date'
        No constructor could take the source type, or constructor overload resolution was ambiguous
F:\study\VC++\Year\Year.cpp(80) : error C2664: '__thiscall Year::Year(class Date)' : cannot convert parameter 1 from 'class Date (__cdecl *)(void)' to 'class Date'
        No constructor could take the source type, or constructor overload resolution was ambiguous
F:\study\VC++\Year\Year.cpp(82) : error C2228: left of '.gety' must have class/struct/union type
F:\study\VC++\Year\Year.cpp(82) : error C2228: left of '.gety' must have class/struct/union type
F:\study\VC++\Year\Year.cpp(89) : error C2512: 'Date' : no appropriate default constructor available
F:\study\VC++\Year\Year.cpp(89) : error C2512: 'Date' : no appropriate default constructor available
F:\study\VC++\Year\Year.cpp(93) : error C2064: term does not evaluate to a function
F:\study\VC++\Year\Year.cpp(96) : error C2064: term does not evaluate to a function
F:\study\VC++\Year\Year.cpp(97) : error C2664: 'work' : cannot convert parameter 1 from 'class Date' to 'class Date (__cdecl *)(void)'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

Year.obj - 1 error(s), 0 warning(s)


新人,以前学过一点别的基础语言和c语言,对类和对象很不熟悉,所以可能有各种低端错误,已经自己改了很长时间了但是实在是改不出来。。。求赐教啊TUT!谢谢了~~~~~!!
如果能不怕麻烦帮我把错误的提示解释一下就更好了=w=~
搜索更多相关主题的帖子: include private public 
2012-04-02 23:10
霁色荣光
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-3-24
得分:0 
看着挺唬人的,似乎没加继承吧,全是孤立的类。
2012-04-03 06:26
霁色荣光
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-3-24
得分:0 
#include <iostream>
using namespace std;

int l(int y)
{    if (y%400==0)
        return 1;
    else
        if (y%4==0)
            return 1;
        else
            return 0;
}

class Month
{
public:
    Month(int mm,int ll);
    Month(Month &p){mon=p.mon;l=p.l;}
    int getm(){return mon;}
private:
    int mon,l;
};
Month::Month(int mm,int ll)
{    l=ll;
    switch(mm)
    {    case'1':
        case'3':
        case'5':
        case'7':
        case'8':
        case'10':
        case'12':
            mon=31;
        break;
        case'4':
        case'6':
        case'9':
        case'11':
            mon=30;
        break;
        case'2':
            mon=28+l;
        break;
    }
}

class Date
{
public:
    Date(int yy=0,int mm=0,int dd=0){y=yy;m=mm;d=dd;}        //在这里设置默认参数
    Date(Date &p){y=p.y;m=p.m;d=p.d;}
    int gety(){return y;}
    int getm(){return m;}
    int getd(){return d;}
    void print();                                            //给你加一个成员函数,实现打印日期信息功能
private:
    int y,m,d;
};
void Date::print()
{
    cout<<"Year:"<<y<<' '<<"Month:"<<m<<' '<<"Day:"<<d<<endl;
}
class Year
{
public:
    Year(Date dd);
    Year(Year &p);
    int gety(){return nday;}
private:
    int nday;
    Date d;
};
Year::Year(Date dd):d(dd)
{
    Month m(d.getm(),l(d.gety()));
    int i;
    for(i=1,nday=0;i<d.getm();i++)
    {
        Month m(i,l(d.gety()));        //因为不能调用构造函数,所以构造新的对象,也可以增加成员函数(推荐)
        nday+=m.getm();
    }
    nday+=d.getd();
}
Year::Year(Year &p):d(p.d){nday=p.nday;}

int work(Date d1,Date d2)            //似乎不能直接d1(int,int,int)
{
    Year y1(d1),y2(d2);
    int i,n=0;
    for (i=d1.gety();i<d2.gety();i++)
        n+=365+l(i);
    n=y2.gety()-y1.gety();            //这也错了
    return n;
}

void main()
{
    int yy,mm,dd;
    cout<<"Please enter the beginning date:(yyyymmdd)"<<endl;
    cin>>yy>>mm>>dd;
    Date date1(yy,mm,dd);                    //又是这样,你的类只有构造函数可以用,推荐你再写一个成员函数为参数赋值
    cout<<"Please enter the endding date:(yyyymmdd)"<<endl;
    cin>>yy>>mm>>dd;
    Date date2(yy,mm,dd);
    cout<<"There is "<<work(date1,date2)<<endl;
    cout<<endl<<"days between "<<endl;
    date1.print();
    cout<<"and"<<endl;
    date2.print();
}
2012-04-03 09:31
jojo59516
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-4-2
得分:0 
回复 3楼 霁色荣光
才发现自己没有写注释,不过你居然看完了。。。太谢谢了,根据你说的我来改一下试试~因为都是自学的所以有些概念还比较模糊。。
构造函数只能用来初始化不能被调用是么?本来我是写了一个成员函数的后来去掉了T T
2012-04-03 12:43
jojo59516
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-4-2
得分:0 
回复 3楼 霁色荣光
我已经运行出正确答案了~再次谢谢你~我不仅弄对了程序也更好地理解了构造函数的作用~~好高兴~~
2012-04-03 13:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-365069-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.084346 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved