C++问题,帮我看看,分大大地有
//Date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int=1,int=1,int=1900);
void print() const;//此处为打印函数,声明成const类,有点问题
~Date();
private:
int month;
int day;
int year;
int checkDay(int) const;//验证输入是否合法
};
#endif
//Date.cpp
#include <iostream>
using namespace std;
#include "Date.h "
Date::Date(int mn,int dy,int yr)
{
if(mn> 0 && mn <=12)
month=mn;
else
{
month=1;
cout < < "invalid month( " < <mn < < ")set to 1.\n ";
}
year=yr;
day=checkDay(dy);
cout < < "Date object construtor for date ";
print();//构造函数调用了const成员函数
cout < <endl;
};//end Date construtor
void Date::print() const
{
cout < <month < < '/ ' < <day < < '/ ' < <year;
}
Date::~Date()
{
cout < < "Date object destrutor for date ";
print();//析构函数调用const函数成员
cout < <endl;
}
int Date::checkDay(int testDay) const
{//这个函数可以省略不看,没有问题,就是个验证日期是否合法的
static const int daysPerMonth[13]=
{0,31,28,31,30,31,30,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;
}//end function checkDay
//Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "Date.h "//include Date class definition
class Employee
{
public:
Employee(const char * const,const char * const,const Date &,const Date &);//这个也不太明白,为什么连用两个const,概念有点模糊
void print() const;
~Employee();
private:
char firstName[25];
char lastName[25];
const Date birthDate;
const Date hireDate;
};//end class Employee
#endif
//Employee.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "Date.h "
#include "Employee.h "
Employee::Employee(const char * const first,const char * const last,
const Date &dateOfBirth,const Date &dateOfHire)
:birthDate(dateOfBirth),hireDate(dateOfHire)
//还有次处的两个const,不太清楚
{
int length=strlen(first);
length=(length <25 ? length:24);
strncpy(firstName,first,length);
firstName[length]= '\0 '; //复制字符串没问题可以不看
length=strlen(last);
length=(length <25 ? length:24);
strncpy(lastName,last,length);
lastName[length]= '\0 ';
cout < < "Employee object construtor: " < <firstName < < ' ' < <lastName < <endl;
}//end
void Employee::print() const
{
cout < <lastName < < ", " < <firstName < < " Hired: ";
hireDate.print();
cout < < " Birthday: ";
birthDate.print();
cout < <endl;
}//end function print
Employee::~Employee()
{
cout < < "Employee object destrutor " < <lastName < < ", " < <firstName < <endl;
}//end ~Employee destrutor
//main
#include <iostream>
using namespace std;
#include "Employee.h "
int main()
{
Date birth(7,24,1949);
Date hire(3,12,1989);
Employee manager( "Bob ", "Blue ",birth,hire);
cout < <endl;
manager.print();
cout < < "\nTest Date construtor with inalid values:\n ";
Date lastDayOff(14,35,1994);
cout < <endl;
return 0;
}//end main
报错信息:
--------------------Configuration: fig10_14 - Win32 Debug--------------------
Linking...
fig10_14.obj : error LNK2001: unresolved external symbol "public: __thiscall Employee::~Employee(void) " (??1Employee@@QAE@XZ)
fig10_14.obj : error LNK2001: unresolved external symbol "public: __thiscall Date::~Date(void) " (??1Date@@QAE@XZ)
fig10_14.obj : error LNK2001: unresolved external symbol "public: void __thiscall Employee::print(void)const " (?print@Employee@@QBEXXZ)
fig10_14.obj : error LNK2001: unresolved external symbol "public: __thiscall Employee::Employee(char const * const,char const * const,class Date const &,class Date const &) " (??0Employee@@QAE@QBD0ABVDate@@1@Z)
fig10_14.obj : error LNK2001: unresolved external symbol "public: __thiscall Date::Date(int,int,int) " (??0Date@@QAE@HHH@Z)
Debug/fig10_14.exe : fatal error LNK1120: 5 unresolved externals
执行 link.exe 时出错.
fig10_14.exe - 1 error(s), 0 warning(s)
我觉得问题可能出在那几个const函数成员,可是这样定义是没有错误的呀
求各位帮忙看看吧
[解决办法]
Employee(const char * const,const char * const,const Date &,const Date &);//这个也不太明白,为什么连用两个const,概念有点模糊
const【这个表示指针指向的内容是 const 的,即指针指向的内容不允许修改】 char * const【这个表示指针本身为const,即不可修改指针指向】
[解决办法]
恩, 粗看了一下,
似乎没啥问题 ~
联合编译了没有啊 ?
[解决办法]
你的编译器VC6.0?
把 using namespace std;
换成:
using std::ostream;
这样的形式,问题就可以解决了.
不要再使用这个编译器了,换个吧!
[解决办法]
换gcc。
或dev-c++
或vs2005吧。
赶紧抛弃vc6。
[解决办法]
#include "PhoneNumber.cpp "
------------------->
#include "PhoneNumber.h "
[解决办法]
(??1Employee@@QAE@XZ)
..
LZ好好检查下函数接口和函数声明
[解决办法]
ostream &operator < <(ostream &output,const PhoneNumber &number)
{
output < < "( " < <number.areaCode < < ") " < <number.exchange < < "- " < <number.line;
return output;
}
exchange是私有的吗?上面太多了我没看.如果是私有的,要把这个函数声明为友元啊.
但是我的机器上VC6就算声明了友元也是通不过,我都崩溃了.
[解决办法]
你这个应该没什么错吧,我试过了,有可能是你的软件有问题哦
[解决办法]
class PhoneNumber
{
friend ostream &operator < <(ostream &,const PhoneNumber &)
{
output < < "( " < <number.areaCode < < ") " < <number.exchange < < "- " < <number.line;
return output;
}
friend istream &operator> > (istream &,PhoneNumber &);
private: