LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 742|回复: 1

书上例题,居然错误一大堆!(不会是不支持标准吧?)帮忙编译一下吧?

[复制链接]
发表于 2003-8-9 23:37:34 | 显示全部楼层 |阅读模式
// twoswap.cpp --- specialination overrides a template

#include <iostream>
using namespace std;
template <class T>
void swap (T& a, T& b);

struct job
{
  char name[40];
  double salary;
  int floor;
};

//explicit specialization
template <> void swap<job>(job& j1, job& j2);
void show (job& j);

int main ()
{
  cout.precision(2);
  cout.setf(ios::fixed, ios::floatfield);
  int i = 10, j = 20;
  cout<< "i, j = " << i << ", " << j << ".\n"
    << "Using compiler-generated int swapper: \n";
  swap(i, j);  //generates void swap ( int &, int &)
  cout<< "Now i, j = " << i << ", " << j << ".\n";

  job sue = {"Susan Yaffee", 63000.60, 7};
  job sidney = {"Sidney Taffee", 66060.72, 9};
  cout<< "Before job swapping: \n";
  show (sue);
  show (sidney);
  swap (sue, sidney);  // uses void swap(job&, job&)
  cout<< "After job swapping: \n";
  show(sue);
  show(sidney);

  return 0;
}

template <class T>
void swap(T& a, T& b)
{
  T temp;
  temp = a;
  a = b;
  b = temp;
}

template <> void swap<job>(job& j1, job& j2)  // specialization
{
  double t1;
  int t2;
  t1 = j1.salary;
  j1.salary = j2.salary;
  j2.salary = t1;
  t2 = j1.floor;
  j1.floor = j2.floor;
  j2.floor = t2;
}

void show(job& j)
{
  cout<< j.name << ": $ " << j.salary
    << " on floor " << j.floor << endl;
}
发表于 2003-8-9 23:55:34 | 显示全部楼层
把swap替换成Swap之类的其他名字,swap是std空间里的名字。

也可以把 using namespace std; 换成
using std::cout;
using std::endl;
using std::ios;
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表