新手遇到的很奇怪的问题!!!大侠进来吧

yj19888239 2009-05-04 12:44:42
一开始出现的问题是编译过不了

代码如下:#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);
}
}


...全文
194 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Paradin 2009-05-04
  • 打赏
  • 举报
回复
VC6对友元的支持是不好
打个sp6
或者改成<iostream.h>再去掉using...试试
Paradin 2009-05-04
  • 打赏
  • 举报
回复
第二个版本,把equal的定义和声明放一起试试
yj19888239 2009-05-04
  • 打赏
  • 举报
回复

受教了
那我们考试用的就是VC6~~~这不是要我们命啊
mengde007 2009-05-04
  • 打赏
  • 举报
回复
VS下面木有问题;
baihacker 2009-05-04
  • 打赏
  • 举报
回复


//如下代码在g++中没有问题
//不要用VC6...支持不好...

#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);
}
}
aaaa3105563 2009-05-04
  • 打赏
  • 举报
回复
顶·
Sou2012 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Paradin 的回复:]
VC6对友元的支持是不好
打个sp6
或者改成 <iostream.h>再去掉using...试试
[/Quote]

晕。。
sjsunshinesj 2009-05-04
  • 打赏
  • 举报
回复
我也试试看
bobo_guan 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 Paradin 的回复:]
VC6对友元的支持是不好
打个sp6
或者改成 <iostream.h>再去掉using...试试
[/Quote]

vc6的名字空间和友元有点冲突

不要同时使用
lingyin55 2009-05-04
  • 打赏
  • 举报
回复
rebuild All一下。
T技术沙龙 2009-05-04
  • 打赏
  • 举报
回复
呵呵,后面那个应该是编程软件的自身问题吧,楼主退出环境之后在重新编译估计能过
lpf000 2009-05-04
  • 打赏
  • 举报
回复
带sp6补丁的 就应该可以,不过楼主说不行,你把友元函数放在类前声明看看加2句:
class DayOfYear;
bool equal(DayOfYear date1, DayOfYear date2);
class DayOfYear
{
};
hjjdebug 2009-05-04
  • 打赏
  • 举报
回复
lz 代码在vc6下编译没有问题,我不知道自己是否打sp6补丁,可能没有打。
qzl123666 2009-05-04
  • 打赏
  • 举报
回复
用VS2008一楼的没有问题啊
yj19888239 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Paradin 的回复:]
第二个版本,把equal的定义和声明放一起试试
[/Quote]
还是不行

sp6补丁也打上了~~~也不行
yj19888239 2009-05-04
  • 打赏
  • 举报
回复
我试试~~~
lingyin55 2009-05-04
  • 打赏
  • 举报
回复
打个vc6的补丁看看有没有效果。

[Quote=引用 4 楼 Paradin 的回复:]
第二个版本,把equal的定义和声明放一起试试
[/Quote]

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧