关于名称空间的问题

jiekhui 2004-01-30 03:58:52
源代码如下
//345.h

namespace pers
{
const int LEN=40;
struct Person
{
char fname[LEN];
char lname[LEN];
};
void getPerson(Person &);
void showPerson(const Person &);
}

namespace debts
{
using namespace pers;
struct Debt
{
Person name;
double amount;
};
void getDebt(Debt &);
void showDebt(const Debt &);
double sumdebts(const Debt ar[],int n);
}

#include <iostream>
//#include <string>
using namespace std;
#include "345.h"

namespace pers
{
void getPerson(Person &rp)
{
cout<<"Enter first name: ";
cin>>rp.fname;
cout<<"Enter last name: ";
cin>>rp.lname;
}

void showPerson(const Person &rp)
{
cout<<rp.lname<<","<<rp.fname;
}
}

namespace debts
{
void getDebt(Debt &rd)
{
err: getPerson(rd.name);
cout<<"Enter debt: ";
cin>>rd.amount;
}

void showDebt(const Debt &rd)
{
err: showPerson(rd.name);
cout<<": $"<<rd.amount<<endl;
}

double sumDebts(const Debt ar[],int n)
{
double total=0;
for(int i=0;i<n;i++)
total=+ar[i].amount;
return total;
}
}

#include <iostream>
using namespace std;
#include "345.h"

void other(void);
void another(void);

int main(void)
{
using debts::Debt;
using debts::showDebt;
Debt golf={{"Benny","Goatsniff"},120.0};
showDebt(golf);
other();
another();

return 0;
}

void other(void)
{
using namespace debts;
Person dg={"Doodles","Glister"};
showPerson(dg);
cout<<endl;
err: Debt zippy[3];
int i;

for(i=0;i<3;i++)
getDebt(zipppy[i]);

for(i=0;i<3;i__)
showDebt(zippy[i]);
err: cout<<"Total debt: $"<<sumDebts(zippy,3)<<endl;

return;
}

void another(void)
{
using pers::Person;;

Person collector={"Milo","Rightshift"};
pers::showPerson(collector);
cout<<endl;
}

compile是提示getPerson()和showPerson()未定义;
...全文
20 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
你的getPerson()是在namespace pers{};中定义的
在namespace debts{};中使用它的时候要加限定词
pers::getPerson();
pers::showPerson();

在主函数中使用这两个名称空间的时候要
using namespace pers;
using namespace debts;

或者在每次用这两个名称空间中定义的函数或变量的时候
加限定词
pers::showPerson(rd.name);
debt::Debt zippy[3];
cout<<"Total debt: $"<<debt::sumDebts(zippy,3)<<endl;

这样使用
pacman2000 2004-01-30
  • 打赏
  • 举报
回复
因为是定义在pers中的,但是没有在pers中用,也没指定using namespace,或者using pers::getPerson()
jiekhui 2004-01-30
  • 打赏
  • 举报
回复
怎么没人理俺!
Alan S1 2004-01-30
  • 打赏
  • 举报
回复
MARK

64,649

社区成员

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

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