ここで数時間頭を悩ませていますが、このコードを実行しようとしたときにエラーが発生する理由がまだわかりません。しばらくして、次の式に絞り込むことができました。
pastryPrice()
これが問題の原因です-ご覧のとおり、ソートの1つのテンプレート関数に対して多数のコンパレータを作成しようとしています
struct dialingAreaComp{
inline bool operator()(const Deliver *d1, const Deliver *d2)const {
return d1->getDialingArea() < d2->getDialingArea();
}
};
struct pastryPrice {
inline bool operator()(const Pastry *p1, const Pastry *p2)const {
return p1->getPrice() < p2->getPrice();
}
};
template<class T>
void sortCollection(T& collection)
{
if ( typeid (collection) == typeid(vector <Deliver*>))
{
sort(collection.begin(), collection.end(), dialingAreaComp());
printCollection(collection);
}
else if (typeid (collection) == typeid(vector <Pastry*>))
{
sort(collection.begin(), collection.end(), pastryPrice());
printCollection(collection);
}
else { cout << "WRONG!"; }
}
5 つのエラーが表示されますが、すべて同じです。
重大度コード 説明 Project File Line Suppression State Error C2664 'bool Bakery::pastryPrice::operator ()(const Pastry *,const Pastry *) const': 引数 1 を 'Deliver *' から 'const Pastry *' に変換できません Bakery c :\プログラム ファイル (x86)\マイクロソフト ビジュアル スタジオ 14.0\vc\include\xutility 809
そしてもう1つ:
重大度コード 説明 Project File Line Suppression State Error C2056 illegal expression Bakery c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 809
上で書いた式を外すと、コードは問題なく動作します。1 つのテンプレート関数に 2 つの異なるコンパレータを渡せないのはなぜですか?
今:
C2264 は、互換性のない型のパラメーターを関数に渡そうとしたときに発生するコンパイラ エラーです。
しかし、Deliver 関数は機能し、Deliver コンパレータを外すと、Pastry もコンパイルされました...では、互換性のない型は何ですか?