请教!请教!重载问题

mhcio 2003-11-30 09:00:04
PhoneNumber1.h:
#ifndef PhoneNumber1H
#define PhoneNumber1H
class PhoneNumber{
friend ostream &operator<<( ostream & , const PhoneNumber &); //就是这里
friend istream &operator>>( istream & , PhoneNumber &);
private:
char areaCode[ 4];
char exchange[ 4];
char line[5];
};
#endif

PhoneNumber1.cpp:
#pragma hdrstop
#include "PhoneNumber1.h"
ostream &operator<<( ostream &output, const PhoneNumber &num)
{
output<<'('<<num.areaCode<<") "<<num.exchange<<'-'<<num.line;
return output;
}

istream &operator>>( istream &input, PhoneNumber &num )
{
input.ignore();
input >> setw(4) >> num.areaCode;
input.ignore(2);
input >> setw(4) >> num.exchange;
input.ignore();
input >> setw(5) >> num.line;
return input;
}
#pragma package(smart_init)


Main.cpp:
#pragma hdrstop
#include <iostream>
using namespace std;

#pragma argsused

int main()
{
PhoneNumber phone;

cout<<"Enter phone number in the form(123) 456-7890:\n";
cin>>phone;
cout<<"The phone number entered was: "<<phone<<endl;

return 0;
}
//------------------------------------------------------------------------
这样编译不过去,说是:Friends must be functions or classes

我如果改成一个文件Main.cpp:
#pragma hdrstop
#include <iostream>
#include <iomanip>
using namespace std;
//---------------------------------------------------------------------------

#pragma argsused

class PhoneNumber{
friend ostream &operator<<( ostream & , const PhoneNumber &);
friend istream &operator>>( istream & , PhoneNumber &);
private:
char areaCode[ 4];
char exchange[ 4];
char line[5];
};

ostream &operator<<( ostream &output, const PhoneNumber &num)
{
output<<'('<<num.areaCode<<") "<<num.exchange<<'-'<<num.line;
return output;
}

istream &operator>>( istream &input, PhoneNumber &num )
{
input.ignore();
input >> setw(4) >> num.areaCode;
input.ignore(2);
input >> setw(4) >> num.exchange;
input.ignore();
input >> setw(5) >> num.line;
return input;
}
int main()
{
PhoneNumber phone;

cout<<"Enter phone number in the form(123) 456-7890:\n";
cin>>phone;
cout<<"The phone number entered was: "<<phone<<endl;
return 0;
}

就正常运行
接口要和实现相分离,合在一起并不是好的方法。
第一种错在那里,我想不出来,请各位帮忙:)
...全文
55 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mhcio 2003-12-04
  • 打赏
  • 举报
回复
fireseed(奶油狗【一打开BCB,就象在蹲茅坑!】)
能解释一下为什么吗?
fireseed 2003-12-01
  • 打赏
  • 举报
回复
1. 在你类定义的上面加上:

#include <iostream>
using namespace std;
fierygnu 2003-12-01
  • 打赏
  • 举报
回复
第一种方法friend之前要有类的定义。在头文件的开始包含iostream。
haosjt 2003-12-01
  • 打赏
  • 举报
回复
在类的定义文件中加上 #include <iostream.h>
fireseed 2003-12-01
  • 打赏
  • 举报
回复
#pragma hdrstop是干预编译头用的




另一个请参考:

#pragma package(smart_init)
#pragma package(smart_init, weak)

Description: smart_init argument

The #pragma package(smart_init) assures that packaged units are initialized in the order determined by their dependencies. (Included by default in package source file.) Typically, you would use the #pragma package for .CPP files that are built as packages.

This pragma affects the order of initialization of that compilation unit. For units, initialization occurs in the following order:

1. By their "uses" dependencies, that is, if unitA depends on unitB, unitB must be initialized before unitA.
2. The link order.
3. Priority order within the unit.

For regular .OBJ files (those not built as units), initialization happens first according to priority order and then link order. Changing the link order of the .OBJ files changes the order in which the global object constructors get called.

The following examples show how the initialization differs between units and regular .OBJ files.

Take as an example three unit files, A, B and C that are 搒mart initialized?with #pragma package(smart_init) and have priority values (defined by the priority parameter of the #pragma startup) set of 10, 20 and 30. The functions are named according to their priority value and the parent .OBJ, so the names are a10, a20, a30, b10, and so on.

Since all three are units, and if A uses B and C and the link order is A, B then C, the order of initialization is:

B10 B20 B30 C10 C20 C30 A10 A20 A30

If the above were .OBJ files, not units, the order would be:

A10 B10 C10 A20 B20 C20 A30 B30 C30

The .CPP files that use #pragma package(smart_init) also require that any #pragma link references to other .OBJ files from a .CPP file that declares #pragma package(smart_init), must be resolved by a unit. #pragma link references to non .OBJ files can still be resolved by libraries, etc.

Description: weak packages

The #pragma package(smart_init, weak) directive affects the way an .OBJ file is stored in a package抯 .BPI and .BPL files. If #pragma package(smart_init, weak) appears in a unit file, the compiler omits the unit from BPLs when possible, and creates a non-packaged local copy of the unit when it is required by another application or package. A unit compiled with this directive is said to be 搘eakly packaged?

#pragma package(smart_init, weak) is used to eliminate conflicts among packages that may depend on the same external library.

Unit files containing the #pragma package(smart_init, weak) directive must not have global variables.

For more information about using weak packages, see Weak packaging.



可以给分了吧?
timepalette 2003-12-01
  • 打赏
  • 举报
回复
顺便问一句.
#pragma hdrstop
#pragma package(smart_init)

都是干什么用的呀

64,637

社区成员

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

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