2012-03-26 12 views
0

私はC++を初めて使用しており、スワップのものに詰まっています 以下のコードは、従業員の名前をアルファベット順にソートし、元のものをソートしてソートしたものですが、 スワップメソッドは動作しません printEmployeesの2つの出力excatly同じですが、誰も私を助けることができますか? THXはC++の配列内のオブジェクトをスワップできません

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

あなたの「代入演算子」は実際に何もしません。それを削除すると、デフォルトの 'memeberwise assignment'があなたの望むことを行います。 – Blastfurnace

答えて

0

employee& operator = (const employee &other) 
{employee temp(other); 
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); 
} 
+0

以前はコピーアンドスワップのアプローチを見てきましたが、この実装では重複を 'operator ='から 'swap(...)'メソッドに移すだけではありませんか? – aldo

+0

演算子=を単独で見ると、あるメソッドから別のメソッドに複製が移動していることになります。しかし、クラスのスワップ機能を使用したい場合は便利です。 – Jagannath

+0

良い点! (私は他のもののためにスワップを使っていません。) – aldo

3

このコードが壊れている:問題を解決するあなたの代入演算子を固定し、他の人が話した通り

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

偉大な、それは多くのthxを動作する、btwは、新しい従業員を作成し、それに既知の従業員のデータをコピーすると同じことを行うオペレータですか? – user1292078

+0

私はそれを得た!このメソッドは、このクラスでどのオペレータが "="何をするかを定義することを意味しますか? – user1292078

+0

はい。それでおしまい。 –

関連する問題