2012-04-28 12 views
1

ベクトルオブジェクトのアルゴリズムライブラリのfind関数を使用します。ベクトルオブジェクトは顧客のベクトルです(顧客は私が作ったクラスです)。私は最初にそれを実行し、それは私に​​のエラーを与えた。私はそれをウェブで検索し、ここでもそれを検索しました。私はここでそれに関する質問を見つけ、同じコードを実行しましたが、まだエラーがあります。ベクトルのアルゴリズムからのfind関数を使用する

私のコードはここにある:

ヘッダーファイル:

#include <string> 
#include <sstream> 
#include <map> 
#include <vector> 

using namespace std; 

enum Status {ACTIVE, INACTIVE}; 

class Customer { 
    private: 
     // ID Database class for storing customers' ids 
     class IdDB { 
      private: 
       friend class Customer; 

       // member field 
       static map <string, int> idList; 

       // member function 
       static int getNumber (const string &threeLetters) { 
        map<string, int>::iterator i = idList.find(threeLetters); 

        if (i == idList.end()) { 
         idList.insert(pair <string, int> (threeLetters, 0)); 
         return 0; 
        }else{ 
         return ++(i->second); 
        } 
       } 
     }; 

     string id; 
     string name; 
     string address; 
     Status status; 

     void makeId() { 
      string threeLetters = name.substr(0, 3); 
      int idNum = IdDB::getNumber(threeLetters); 

      stringstream oss; 
      oss << threeLetters << idNum; 
      id = oss.str(); 
     } 

    public: 
     Customer (const string&, const string&, const Status); 

     // Accessor Methods 
     string &getId(); 
     string &getName(); 
     string &getAddress(); 
     Status getStatus(); 

     // Mutator Methods 
     void setAddress (const string&); 
     void setStatus (const Status); 

     // Misc. Methods 
     void printStatus(); 

     // Equality Operator Overloading 
     friend bool operator == (Customer&, Customer&); 
}; 

class CustomerDB { 
    private: 
     static vector<Customer> customersList; 

    public: 
     static void addCustomer (const Customer&); 
     static void deleteCustomer (Customer&); 
}; 

ソースコード:

#include <iostream> 
#include <string> 
#include <algorithm> 
#include "Customer.h" 

using namespace std; 

map<string, int> Customer::IdDB::idList; 

Customer::Customer (const string &cName, const string &cAddress, const Status cStatus) : name(cName), address(cAddress), status(cStatus) { 
    makeId(); 
} 

// Accessor Methods 
string &Customer::getId() { return id; } 
string &Customer::getName() { return name; } 
string &Customer::getAddress() { return address; } 
Status Customer::getStatus() { return status; } 

// Mutator Methods 
void Customer::setAddress (const string &newAddress) { address = newAddress; } 
void Customer::setStatus (const Status newStatus) { status = newStatus; } 

// Misc. Methods 
void Customer::printStatus() { 
    if (status == ACTIVE) 
     cout << "Active"; 
    else 
     cout << "In-Active"; 
} 

vector<Customer> CustomerDB::customersList; 

void CustomerDB::addCustomer (const Customer &customer) { 
    customersList.push_back(customer); 
} 

void CustomerDB::deleteCustomer (Customer &customer) { 
    vector<Customer>::iterator i; 
    i = find(customersList.begin(), customersList.end(), customer); // getting error in here 
} 

// Equality Operator Overloading 
bool operator == (Customer &cust1, Customer &cust2) { 
    return cust1.getId() == cust2.getId(); 
} 

コード::ブロックを構築した後、私はこれを得た、

でヘッダファイル​​:

} 

    /// This is an overload used by find() for the RAI case. 
    template<typename _RandomAccessIterator, typename _Tp> 
    _RandomAccessIterator 
    __find(_RandomAccessIterator __first, _RandomAccessIterator __last, 
     const _Tp& __val, random_access_iterator_tag) 
    { 
     typename iterator_traits<_RandomAccessIterator>::difference_type 
    __trip_count = (__last - __first) >> 2; 

     for (; __trip_count > 0; --__trip_count) 
    { 
     if (*__first == __val) // error in here exactly getting a red block 
     return __first; 
     ++__first; 

     if (*__first == __val) 
     return __first; 
     ++__first; 

     if (*__first == __val) 
     return __first; 

おかげ

編集:ここではビルドログは、彼らが実際にはエラーではありませんが

Compiling: C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp 
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/algorithm:63:0, 
       from C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:5: 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h: In function '_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Customer*, std::vector<Customer> >, _Tp = Customer]': 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:4403:45: instantiated from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<Customer*, std::vector<Customer> >, _Tp = Customer]' 
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:42:66: instantiated from here 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:162:4: error: no match for 'operator==' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Customer*, _Container = std::vector<Customer>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Customer&]() == __val' 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:162:4: note: candidates are: 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2448:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2460:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:309:5: note: template<class _Val> bool std::operator==(const std::_Rb_tree_iterator<_Tp>&, const std::_Rb_tree_const_iterator<_Val>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:846:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator==(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_map.h:877:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multimap.h:795:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1273:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) 
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: bool operator==(Customer&, Customer&) 
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: no known conversion for argument 2 from 'const Customer' to 'Customer&' 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:4403:45: instantiated from '_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<Customer*, std::vector<Customer> >, _Tp = Customer]' 
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:42:66: instantiated from here 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:166:4: error: no match for 'operator==' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Customer*, _Container = std::vector<Customer>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Customer&]() == __val' 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:166:4: note: candidates are: 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2448:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2460:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:309:5: note: template<class _Val> bool std::operator==(const std::_Rb_tree_iterator<_Tp>&, const std::_Rb_tree_const_iterator<_Val>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_tree.h:846:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator==(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_map.h:877:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_multimap.h:795:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1273:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) 
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: bool operator==(Customer&, Customer&) 
C:\Users\KiKo-SaMa\Desktop\C++\DVD_App\Customer.cpp:46:6: note: no known conversion for argument 2 from 'const Customer' to 'Customer&' 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:170:4: error: no match for 'operator==' in '__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Customer*, _Container = std::vector<Customer>, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = Customer&]() == __val' 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_algo.h:170:4: note: candidates are: 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) 
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&) 
Process terminated with status 1 (0 minutes, 1 seconds) 
50 errors, 0 warnings 
+1

正確にはどのようなエラーですか? – Fanael

+0

コマンドプロンプトを使用してコンパイルすると、何かの根底にあるコードではなく、ビルド時にエラーが発生します。 –

+1

エラーを見ることなく、 'operator =='は次のようになります。 'Customer&'オブジェクトの代わりに 'const Cusstomer&'オブジェクトを取る。 – Chad

答えて

2

等価コンパレータはconst以外の参照で引数を取るが、findの最後の引数はconst参照になり、コンパイラでは使用できないという問題があります。

ちなみに、constを追加すると、データにconstアクセサを追加することが強制されます。また、オペレータがパブリックインターフェイスのみを使用している場合、それを友人として宣言する必要はありません。

+0

アクセサー関数もconstにしましたが、 'string&&'だけを返す関数に ''無効な初期化の初期化 'を受け取りました –

+0

@ aizen92: 'const'と宣言された関数は非const参照を返すことができません。契約を破るだろう。あなたは 'const std :: string&'を返す必要があります(別の注意:ヘッダーに 'using directives'を使用しないでください!)。 mutatingとconstアクセサーの両方が必要な場合は、両方を提供する必要があります。 –

0

コードブロックは、STLファイルに関するエラーを与える可能性があります。あなたのエラーの関連stlファイルを表示します。

+0

本当に答えは – nikhil

+0

はい、このスレッドに最適な答えではありませんが、他のコンパイラに関してCodeBlocksのさまざまなエラーメカニズムについて言及したいだけです。 – phantasmagoria

+0

これはどんなコメントです。 – nikhil

関連する問題