首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

为什么会出现下面的有关问题

2012-04-24 
为什么会出现下面的问题?//Date.hclass Date{public :Date(int 1,int 1,int 1900)//默认构造函数vo

为什么会出现下面的问题?
//Date.h
class Date
{
public :
  Date(int = 1,int = 1,int = 1900);//默认构造函数

  void setMonth(int); //私有成员设定
  void setDay(int);
  void setYear(int);

  void print() const ; //以MONTH/DAY/YEAR格式输出
  ~Date(); //
private:
  int month ;
  int day ;
  int year ;

  int checkDay(int ) const ;
};
//Date.cpp

#include <iostream>
using std::cout;
using std::endl;

#include "Date.h"

Date::Date(int mn, int dy, int yr)
{
  setMonth(mn);
  setDay(dy);
  setYear(yr);
  cout << "Date object constructor for date";
  print() ;
  cout <<endl;
}

void Date::setDay(int dy)
{
  day = checkDay(dy);
}

void Date::setYear(int yr)
{
  year = yr;
}

void Date::setMonth(int mn)
{
  if(mn > 0 && mn <=12)
  {
  month = mn ;
  }
  else
  {
  month = 1;
  cout << "Invalid month (" << mn << ") set to 1. \n ";
  }
}

void Date::print() const
{
  cout << month << '/' <<day <<'/' <<year;
}

Date::~Date()
{
  cout << "Date object destructor for date ";
  print();
  cout << endl;
}

int Date::checkDay(int testDay) const
{
  static const int daysPerMonth[13]=
  {0,31,28,31,30,31,30,31,31,30,31,30,31};

  if(testDay > 0 && testDay <= daysPerMonth[month])
  return testDay;
  if(month == 2 && testDay == 29 && (year % 400 ==0 || (year % 4 == 0 && year % 100 == 0)))
  return testDay;
  cout << " Invalid day (" << testDay << ") set to 1 .\n";
  return 1;
}
//Employee.h

#include "Date.cpp"

class Employee
{
  public :
  Employee(const char * const ,const char *const
  ,const Date &, const Date &);
  void print() const ;
  ~Employee();

  private:
  char firstName[25];
  char lastName[25];
  const Date birthDate;
  const Date hireDate ;
};
//Employee.cpp

#include <iostream>
using std::endl;
using std::cout ;
using std::cin;

#include <cstring >
using std::strlen;
using std::strncpy;

#include "Employee.h"

Employee::Employee(const char * const first,const char * const last
  , const Date &dateOfBirth,const Date &dateOfHire)
:birthDate(dateOfBirth),hireDate(dateOfHire)
{
  int length = strlen(last);
  length = (length < 25 ? length :24);
  strncpy(lastName,last ,length);
  lastName[length] = '\0';

  cout << "Employee object constructor :"
  << firstName << ' ' <<lastName <<endl;
}

void Employee ::print() const
{
  cout << lastName << ", " <<firstName << "Hired :";
  hireDate.print();
  cout << " BirthDay :";
  birthDate.print();
  cout << endl;
}

Employee::~Employee()
{
  cout << "Employee object destructor :"
  << lastName << ", " <<firstName << endl;


}

//main.cpp
#include <iostream>
using std::endl;
using std::cout;


#include "Employee.h"

int main()
{
  Date birth(7,24,1949);
  Date hire (3,12,1988);
  Employee manager("Bob", "Blue",birth,hire);

  cout << endl;
  manager.print();

  cout << "\n Test Date constructor with invalid values:\n";
  Date lastDayOff(14,35,1994);
  cout <<endl;
  return 0;
}
一个简单的小程序,但是为什么在编译的时候会出现multiple definition of Date::Date(……),……,在Date.cpp中,这是什么原因啊?


[解决办法]
我把上述代码放在一个cpp里面编译,是没有问题的。
[解决办法]
//Employee.h

#include "Date.cpp"

是不是应该包含头文件啊?
[解决办法]
//Employee.h

#include "Date.cpp"

class Employee

这个地方为什么包含的是cpp文件...
[解决办法]
#include "Date.cpp"

这种写法还是第一次看见呢,包含一般都是包含.h文件,估计问题就出在这里了
[解决办法]
#include "Date.cpp"
是不是写错了#include "Date.h" ??
[解决办法]

探讨
#include "Date.cpp"

这种写法还是第一次看见呢,包含一般都是包含.h文件,估计问题就出在这里了

[解决办法]
C/C++ code
#include "Date.cpp"//这个你引用错了吧,应该是.h才对吧
[解决办法]
你把Date(int = 1,int = 1,int = 1900);改成
Date::Date(int mn=1, int dy=1, int yr=1900);试下
[解决办法]
可以啊,但是我发现输出时有点问题,因为你在employee类里面只有对lastname进行处理,而没有对firstname进行处理

热点排行