簡體   English   中英

無法在C ++中交換數組中的對象

[英]cannot swap objects in an array in C++

我是C ++的新手,並且陷入了交換工作中,下面的代碼是按字母順序對員工姓名進行排序的程序,並打印出原始的並按順序排序,但是swap方法對printEmployees的兩個輸出無效,同樣,有人可以幫助我嗎? 謝謝

    #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;
}

此代碼已損壞:

 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;
}

正如其他人所說,修復您的分配運算符將解決該問題。
我看到您嘗試根據副本構造函數實現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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM