![]() |
#2
rjsp2015-01-30 08:32
|

//namespace.h
#include <string>
namespace pers
{
struct Person
{
std::string fname;
std::string lname;
};
void getPerson (Person &);
void showPerson (Person &);
}
namespace debts
{
using namespace pers;
struct Debt
{
Person name;
double amount;
};
void getDebt (Debt &);
void showDebt (Debt &);
double sumDebts (const Debt ar[],int n);
}
#include <string>
namespace pers
{
struct Person
{
std::string fname;
std::string lname;
};
void getPerson (Person &);
void showPerson (Person &);
}
namespace debts
{
using namespace pers;
struct Debt
{
Person name;
double amount;
};
void getDebt (Debt &);
void showDebt (Debt &);
double sumDebts (const Debt ar[],int n);
}

//namesp.cpp
#include <iostream>
#include "namespace.h"
namespace pers
{
using std::cout;
using std::cin;
using std::endl;
void getPerson (Person &rp)
{
cout << "Enter the first name : ";
cin >> rp.fname;
cout << endl;
cout << "Enter the last name : ";
cin >> rp.lname;
cout << endl;
}
void showPerson (const Person &rp)
{
cout << rp.lname << " and " << rp.fname;
cout << endl;
}
}
namespace debts
{
using std::cout;
using std::cin;
using std::endl;
using pers::getPerson;
using pers::showPerson;
void getDebt (Debt &rd)
{
getPerson (rd.name);
cout << "Enter debt :";
cin >> rd.amount;
}
void showDebt (const Debt &rd)
{
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>
#include "namespace.h"
namespace pers
{
using std::cout;
using std::cin;
using std::endl;
void getPerson (Person &rp)
{
cout << "Enter the first name : ";
cin >> rp.fname;
cout << endl;
cout << "Enter the last name : ";
cin >> rp.lname;
cout << endl;
}
void showPerson (const Person &rp)
{
cout << rp.lname << " and " << rp.fname;
cout << endl;
}
}
namespace debts
{
using std::cout;
using std::cin;
using std::endl;
using pers::getPerson;
using pers::showPerson;
void getDebt (Debt &rd)
{
getPerson (rd.name);
cout << "Enter debt :";
cin >> rd.amount;
}
void showDebt (const Debt &rd)
{
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;
}
}

//main.cpp
#include <iostream>
#include "namespace.h"
void other (void);
void another (void);
int main ()
{
using debts::Debt;
using debts::showDebt;
Debt golf = {{"Benny","Goatsniff"},120.0};
showDebt (golf);
other ();
another ();
return 0;
}
void other (void)
{
using std :: cout;
using std :: cin;
using std :: endl;
using namespace debts;
Person dg = {"Doodles","Glister"};
showPerson (dg);
cout << endl;
Debt zippy[3];
int i = 0;
for (i = 0;i < 3;i++)
{
getDebt (zippy[i]);
}
for (i = 0;i < 3;i++)
{
showDebt (zippy[i]);
}
cout << "Total debt : $" <<sumDebts (zippy,3) << endl;
return ;
}
void another (void)
{
using pers::Person;
Person collector = {"Milo","Rightshitf"};
pers::showPerson (collector);
std::cout << std::endl;
return ;
}
#include <iostream>
#include "namespace.h"
void other (void);
void another (void);
int main ()
{
using debts::Debt;
using debts::showDebt;
Debt golf = {{"Benny","Goatsniff"},120.0};
showDebt (golf);
other ();
another ();
return 0;
}
void other (void)
{
using std :: cout;
using std :: cin;
using std :: endl;
using namespace debts;
Person dg = {"Doodles","Glister"};
showPerson (dg);
cout << endl;
Debt zippy[3];
int i = 0;
for (i = 0;i < 3;i++)
{
getDebt (zippy[i]);
}
for (i = 0;i < 3;i++)
{
showDebt (zippy[i]);
}
cout << "Total debt : $" <<sumDebts (zippy,3) << endl;
return ;
}
void another (void)
{
using pers::Person;
Person collector = {"Milo","Rightshitf"};
pers::showPerson (collector);
std::cout << std::endl;
return ;
}
编译器错误提示:>main.obj : error LNK2019: 无法解析的外部符号 "void __cdecl debts::showDebt(struct debts::Debt &)" (?showDebt@debts@@YAXAAUDebt@1@@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "void __cdecl pers::showPerson(struct pers::Person &)" (?showPerson@pers@@YAXAAUPerson@1@@Z),该符号在函数 "void __cdecl other(void)" (?other@@YAXXZ) 中被引用
1>E:\Code\code from C++ Primer Plus\名称空间示例代码\Debug\名称空间示例代码.exe : fatal error LNK1120: 2 个无法解析的外部命令
这是怎么回事啊,查资料查不到。。