新手遇到的很奇怪的问题!!!大侠进来吧
一开始出现的问题是编译过不了
代码如下:#include <iostream>
using namespace std;
class DayOfYear
{
public:
friend bool equal(DayOfYear date1, DayOfYear date2);
DayOfYear(int the_month, int the_day);
DayOfYear(){}
void input();
void output();
int get_month();
int get_day();
private:
void check_date();
int month;
int day;
};
int main()
{
DayOfYear today,yj_birthday(8,23);
cout<<"enter today's date\n";
today.input();
cout<<"yj's birthday is";
yj_birthday.output();
if(equal(today,yj_birthday))
cout<<"happy birthday yangjie!\n";
else
cout<<"happy unbirthday yangjie!\n";
return 0;
}
bool equal(DayOfYear date1, DayOfYear date2)
{
return(date1.month==date2.month&&date1.day==date2.day);
}
DayOfYear::DayOfYear(int the_month, int the_day)
{
month=the_month;
day=the_day;
check_date();
}
int DayOfYear::get_month()
{
return month;
}
int DayOfYear::get_day()
{
return day;
}
void DayOfYear::input()
{
cout<<"enter the month as a number:";
cin>>month;
cout<<"enter the day of the month:";
cin>>day;
}
void DayOfYear::output()
{
cout<<"month="<<month
<<",day="<<day<<endl;
}
void DayOfYear::check_date()
{
if((month<1)|(month>12)|(day<1)|(day>31))
{
cout<<"Illegal date. aborting program.\n";
exit(1);
}
}
出错如下:
--------------------Configuration: 11_2 - Win32 Debug--------------------
Compiling...
11_2.cpp
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(43) : error C2248: 'month' : cannot access private member declared in class 'DayOfYear'
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(20) : see declaration of 'month'
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(43) : error C2248: 'month' : cannot access private member declared in class 'DayOfYear'
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(20) : see declaration of 'month'
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(43) : error C2248: 'day' : cannot access private member declared in class 'DayOfYear'
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(21) : see declaration of 'day'
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(43) : error C2248: 'day' : cannot access private member declared in class 'DayOfYear'
C:\Documents and Settings\Administrator\桌面\VC精装版\VC精装版\MyProjects\11_2\11_2.cpp(21) : see declaration of 'day'
执行 cl.exe 时出错.
11_2.obj - 1 error(s), 0 warning(s)
然后有人说要把友元函数的声明放到了里面去,我改了过后编译没问题但是连接出错
代码如下:
#include <iostream>
using namespace std;
class DayOfYear
{
public:
friend bool equal(DayOfYear date1, DayOfYear date2);
DayOfYear(int the_month, int the_day);
DayOfYear(){}
void input();
void output();
int get_month();
int get_day();
private:
void check_date();
int month;
int day;
bool equal(DayOfYear date1, DayOfYear date2)
{
return(date1.month==date2.month&&date1.day==date2.day);
}
};
省略了的同上
出错问题:
--------------------Configuration: 11_2 - Win32 Debug--------------------
Linking...
11_2.obj : error LNK2001: unresolved external symbol "bool __cdecl equal(class DayOfYear,class DayOfYear)" (?equal@@YA_NVDayOfYear@@0@Z)
Debug/11_2.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
11_2.exe - 1 error(s), 0 warning(s)
然后又听高手说后把private改成public编译和连接都没问题了
但是没用到private的话不就没意义了吗?
我就是想学习运用一下友元函数和private
代码如下:
#include <iostream>
using namespace std;
class DayOfYear
{
public:
friend bool equal(DayOfYear date1, DayOfYear date2);
DayOfYear(int the_month, int the_day);
DayOfYear(){}
void input();
void output();
int get_month();
int get_day();
public:
void check_date();
int month;
int day;
};
int main()
{
DayOfYear today,yj_birthday(8,23);
cout <<"enter today's date\n";
today.input();
cout <<"yj's birthday is";
yj_birthday.output();
if(equal(today,yj_birthday))
cout <<"happy birthday yangjie!\n";
else
cout <<"happy unbirthday yangjie!\n";
return 0;
}
bool equal(DayOfYear date1, DayOfYear date2)
{
return(date1.month==date2.month&&date1.day==date2.day);
}
DayOfYear::DayOfYear(int the_month, int the_day)
{
month=the_month;
day=the_day;
check_date();
}
int DayOfYear::get_month()
{
return month;
}
int DayOfYear::get_day()
{
return day;
}
void DayOfYear::input()
{
cout <<"enter the month as a number:";
cin>>month;
cout <<"enter the day of the month:";
cin>>day;
}
void DayOfYear::output()
{
cout <<"month=" <<month
<<",day=" <<day <<endl;
}
void DayOfYear::check_date()
{
if((month <1)|(month>12)|(day <1)|(day>31))
{
cout <<"Illegal date. aborting program.\n";
exit(1);
}
}