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

“日期”类的运用出错了

liusonglin 发布于 2014-03-25 18:13, 410 次点击
在下面这个程序中有一个错误,总是不能运行!
#include <iostream>
using namespace std;
class Date
{
public:
    Date();
    Date (int y,int m,int d)
    {   year=y;
        month=m;
        day=d;
        cout<<"Constructor called!"<<endl;
    }

    ~Date()
    { cout<<"Destructor called!"<<endl; }

    void Date::set_date()
{ cin>>year>>month>>day; }

    void display_date()
    { cout<<year<<" "<<month<<" "<<day<<endl; }

private:
    int year;
    int month;
    int day;
};

int main()
{     
    Date d1;
    d1.set_date();
    Date d2(2014,3,27);
    d1.display_date();
    d2.display_date();
    return 0;
}

Linking...
Date.obj : error LNK2001: unresolved external symbol "public: __thiscall Date::Date(void)" (??0Date@@QAE@XZ)
Debug/Date.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.

Date.exe - 1 error(s), 0 warning(s)
请各位大神帮忙看看,小弟谢谢了!
3 回复
#2
gpf2014-03-25 20:44
#include <iostream>
using namespace std;
class Date
{
public:
    Date(){};//这里缺个大括号
    Date (int y,int m,int d)
    {   
        year=y;
        month=m;
        day=d;
        cout<<"Constructor called!"<<endl;
    }

    ~Date()
    {
        cout<<"Destructor called!"<<endl;
    }

    void set_date()//在类里面定义,不需要作用域运算符
    {
        cin>>year>>month>>day;
    }

    void display_date()
    {
        cout<<year<<" "<<month<<" "<<day<<endl;
    }

private:
    int year;
    int month;
    int day;
};

int main()
{     
    Date d1;
    d1.set_date();
    Date d2(2014,3,27);
    d1.display_date();
    d2.display_date();
    return 0;
}
#3
liusonglin2014-03-25 20:54
回复 2楼 gpf
首先谢谢你的指点,但是这样运行怎么没有输出结果呀?
#4
liusonglin2014-03-25 21:03
回复 2楼 gpf
谢谢你!我知道了
1