2017-10-25 11 views
2

を動作させることはできません。過負荷=演算子は、私は以下のコードでは9行に=演算子をオーバーロードしようとしています、それは

void searchContact(vector<Person> &people){ 
    string searchTerm; 
    vector<Person>::iterator it; 

    cout << endl; 
    cout << "Enter search term: "; 
    getline(cin, searchTerm); 

    it = find(people.begin(), people.end(), searchTerm); 

    if (it != people.end()){ 
     cout << "Element found in: " << *it << '\n'; 
    }else{ 
     cout << "Element not found\n"; 
    } 
} 

私のアプローチはこれです:私は

int data; 

    Person& operator=(Person& a) { return a; } 
    Person& operator=(int a) { 
    data = a; 
    return *this; 
    } 

このエラーを取得しています:

class.cpp:129:30: error: ‘Person& operator=(Person&)’ must be a nonstatic member function 
    Person& operator=(Person& a) { return a; } 
          ^
class.cpp:130:26: error: ‘Person& operator=(int)’ must be a nonstatic member function 
    Person& operator=(int a) { 

何私のアプローチが間違っているか、私は最初からそれがすべて間違っているのでしょうか?

+4

はあなたが*代入*演算子を作成したいあなたは確かにいますか?そして、等価の '=='演算子ではないのですか? –

+0

入手したエラーについては、[最小限の、完全で検証可能な例](http://stackoverflow.com/help/mcve)を作成してください。 –

+0

最後に、あなたのコピー代入演算子が間違っているので([オーバーロードされた演算子の標準実装のこのリファレンス](http://en.cppreference.com/w/cpp/language/operators#Canonical_implementations)を読むことをお勧めします初心者には何もコピーしない)。 –

答えて

0

ファースト間違った演算子に過負荷がかかります。 std::find()operator=(代入)の代わりにoperator==(比較)を使用します。 std::stringstd::find()に渡す場合、Personではなく、std::stringを入力とするoperator==が必要です。

第2に、演算子を単項演算子として実装しようとしています。つまり、演算子はPersonクラスの非静的メンバーでなければなりません。コンパイラは、そうではないと不平を言っています。 std::find()が一致を見つけた場合

第三に、あなたはstd::cout*itを渡しているので、あなたは、出力のためPersonを取るオーバーロードさoperator<<を必要としています。

このような何かを試してみてください:

class Person 
{ 
public: 
    ... 

    bool operator==(const string &rhs) const 
    { 
     // compare members of *this to rhs as needed... 
     return ...; // true or false 
    } 

    /* alternatively: 
    friend bool operator==(const Person &lhs, const string &rhs) 
    { 
     // compare members of lhs to rhs as needed... 
     return ...; // true or false 
    } 
    */ 

    friend ostream& operator<<(ostream &out, const Person &p) 
    { 
     // output p to out as needed... 
     return out; 
    } 

    ... 
}; 

そして、検索コードは動作します:

void searchContact(vector<Person> &people) 
{ 
    cout << endl; 
    cout << "Enter search term: "; 

    string searchTerm; 
    getline(cin, searchTerm); 

    vector<Person>::iterator it = find(people.begin(), people.end(), searchTerm);  

    if (it != people.end()) { 
     cout << "Element found in: " << *it << '\n'; 
    } else { 
     cout << "Element not found\n"; 
    } 
} 
0
#include <iostream> 
#include <vector> 
#include <algorithm> 

using namespace std; 

class Person{ 

private: 
    int data; 

public: 
    Person(int data) 
    { 
     this->data = data; 
    } 

    int getData() 
    { 
     return data; 
    } 

    friend bool operator==(const Person &lhs, const int rhs); 
}; 
bool operator== (const Person &lhs, const int rhs) 
{ 
    return lhs.data == rhs; 
} 

void searchContact(std::vector<Person> &people){ 
    int searchTerm = 1; 
    vector<Person>::iterator it; 

    it = find(people.begin(), people.end(), searchTerm); 

    if (it != people.end()){ 
     cout << "Element found in: " << it->getData() << '\n'; 
    }else{ 
     cout << "Element not found\n"; 
    } 
} 



int main(int argc, char **argv) 
{ 
    std::vector<Person> list1 = {1, 2, 3, 4}; 
    std::vector<Person> list2 = {1, 2, 3, 5}; 
    std::vector<Person> list3 = {1, 3, 7, 6, 9, 5, 2, 4}; 

    searchContact(list1); 
    searchContact(list2); 
    searchContact(list3); 
} 

あなたは何をしたいですか?あなたの完全なコードを表示し、何をしたいかを教えてください。

あなたは、ベクトルの要素を検索する場合、あなたはこのように書くことができます(文字列ではありません。OUは、文字列を検索したい場合は、単に文字列にint型に変更し、CINまたはなどとのデータを取得する)あなたは、

+0

'(* it).getData()'は 'it-> getData()'と書くこともできます –

+0

ああ、私はそれを忘れてしまった –

関連する問題