无法在C ++中交换数组中的对象

时间:2012-03-26 03:14:21

标签: c++ swap

我是C ++的新手,并且陷入了互换的境地  下面的代码是按照字母顺序排序员工姓名的程序,并打印出原始的和排序的一个,但是  交换方法不起作用  printEmployees的两个输出非常相似,任何人都可以帮助我吗? THX

    #include <iostream>

    #include <string>

    #include <iomanip>

    #include <algorithm>
using namespace std;


class employee
{

   /* Employee class to contain employee data
   */

   private:
     string surname;
     double hourlyRate;
     int empNumber; 
   public:
      employee() {
         hourlyRate = -1;
         empNumber = -1;
         surname = "";
      }
      employee(const employee &other) :
         surname(other.surname),
         hourlyRate(other.hourlyRate),
         empNumber(other.empNumber){}

      void setEmployee(const string &name, double rate,int num);
      string getSurname() const;
      void printEmployee() const;
     employee& operator = (const employee &other)
    {employee temp(other);
     return *this;}};    

     void employee::setEmployee(const string &name, double rate, int num) {   
      surname = name;
      hourlyRate = rate;
      empNumber = num;
      }
     string employee::getSurname() const { return surname; }  
     void employee::printEmployee() const {
         cout << fixed;
         cout << setw(20) << surname << setw(4) << empNumber << "  " << hourlyRate << "\n";
      }

  void printEmployees(employee employees[], int number)
   {
   int i;
    for (i=0; i<number; i++) { employees[i].printEmployee();   }
   cout << "\n";
   }


void swap(employee employees[], int a, int b)
{
  employee temp(employees[a]);
employees[a] = employees[b];
employees[b] = temp;

}


void sortEmployees(employee employees[], int number)
{
   /* use selection sort to order employees, 
      in employee 

name order
   */


    int inner, outer, max;


    for (outer=number-1; outer>0; outer--)
   {
      // run though array number of times
      max = 0;
      for (inner=1; 

inner<=outer; inner++)
      {
         // find alphabeticaly largest surname in section of array
         if (employees

[inner].getSurname() < employees[max].getSurname())
            max = inner;
      }
      if (max != outer)
      {
         // 

swap largest with last element looked at in array
         swap(employees, max, outer);
      }
   }
}


int main()
{
   employee employees[5];

   employees[0].setEmployee("Stone", 35.75, 053);
   employees[1].setEmployee

("Rubble", 12, 163);
   employees[2].setEmployee("Flintstone", 15.75, 97);
   employees[3].setEmployee("Pebble", 10.25, 104);


  employees[4].setEmployee("Rockwall", 22.75, 15);


   printEmployees(employees, 5);

   sortEmployees(employees,5);
   printEmployees(employees, 5);

   return 0;
}

2 个答案:

答案 0 :(得分:3)

此代码已损坏:

 employee& operator = (const employee &other)
{employee temp(other);
 return *this;}

应该是这样的:

employee& operator= (const employee &other)
{
   surname = other.surname;
   hourlyRate = other.hourlyRate;
   empNumber = other.empNumber;
   return *this;
}

答案 1 :(得分:0)

正如其他人所说,修理任务操作员将解决问题 我看到你试图在复制构造函数方面实现operator=但是错过了交换。如果要避免复制构造函数和赋值运算符中的代码重复,可以尝试以下方法。

employee& operator=(const employee& other)
{
    employee temp(other);
    swap(temp);
    return *this;
}

void swap(employee& other)
{
     std::swap(surname, other.surname);
     std::swap(hourlyRate, other.hourlyRate);
     std::swap(empNumber, other.empNumber);
}