我是 OOP 的初学者->我有一个带有 3 个私有变量成员的 Date 类,并且应该以 2 种方式打印日期:
- 2010 年 12 月 25 日
- 2010 年 12 月 25 日
以下代码给出了错误:
date.obj:错误 LNK2019:未解析的外部符号“public:__thiscall Date::Date(void)”(??0Date@@QAE@XZ)在函数“public:void __thiscall Date::printDate(void)”中引用(? printDate@Date@@QAEXXZ) 我做错了什么?日期.h
#include<iostream>
#include<string>
#ifndef DATE_H
#define DATE_H
class Date
{
private:
int day;
int month;
int year;
public:
Date();
Date(int d, int m, int y)
{
day=d;
month=m;
year=y;
}
int getDay() const {return day;}
int getMonth() const {return month;}
int getYear() const {return year;}
void printDate(void);
};
#endif
日期.cpp
#include"date.h"
#include<iostream>
#include<string>
const int NR=12;
void Date::printDate()
{
Date newDate;
std::string Months[]={"January","February", "March" , "April", "May", "June", "July", "August", "September", "Octomber", "November", "December"};
int position;
std::string month;
position=newDate.getMonth();
for(int i=0;i<NR;i++)
{
if(i==position)
{
month=Months[i];
}
}
std::cout<<month<<" "<<newDate.getDay()<<" "<<newDate.getYear()<<std::endl;
}
主文件
#include "date.h"
#include <iostream>
int main()
{
int d;
int m;
int y;
std::cout<<"Enter day: ";
std::cin>>d;
std::cout<<"Enter month: ";
std::cin>>m;
std::cout<<"Enter years: ";
std::cin>>y;
Date newDate(d,m,y);
std::cout<<newDate.getMonth()<<"/"<<newDate.getDay()<<"/"<<newDate.getYear()<<std::endl;
newDate.printDate();
}