亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問:下面程序為什么在連接時有錯,而在不用類模板時又沒有錯?。?/h1>

另外:operator() 、operator== 在程序中是究竟是怎樣被調用的#include <iostream> #include <set> using namespace std; template <class T> class RuntimeCmp { public: enum cmp_mode {normal, reverse}; private: cmp_mode mode; public: //constructor for sorting criterion //-default criterion uses value normal RuntimeCmp (cmp_mode m=normal) : mode(m) { } //comparision of elements bool operator() (const T& t1, const T& t2) const { return mode == normal ? t1 < t2 : t2 < t1; } //comparision of sorting criteria bool operator== (const RuntimeCmp& rc) { return mode == rc.mode; } }; //type of a set that uses this sorting criterion typedef set<int,RuntimeCmp<int> > IntSet; void fill (IntSet& Set) { //fill insert elements in random order Set.insert(4);  Set.insert(7); Set.insert(5); Set.insert(1); Set.insert(6); Set.insert(2); Set.insert(5); } template <class T> int main() { //create, fill, and print set with normal element order //-uses default sorting criterion IntSet coll1; fill(coll1); copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));cout<<endl;RuntimeCmp<int> reverse_order(RuntimeCmp<int>::reverse); //create, fill, and print set with reverse element order IntSet coll2(reverse_order);  fill(coll2); copy(coll2.begin(),coll2.end(),ostream_iterator<int>(cout," "));cout<<endl;//assign elements AND sorting criterion coll1 = coll2; coll1.insert(3); // PRINT_ELEMENTS coll1copy(coll1.begin(),coll1.end(),ostream_iterator<int>(cout," "));cout<<endl;//just to make sure... if (coll1.value_comp() == coll2.value_comp()) { cout << "coll1 and coll2 have same sorting criterion" << endl; } else { cout << "coll1 and coll2 have different sorting criterion" << endl; } return 0;}
查看完整描述

2 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

根據你的出錯信息來看,要么你沒有main,要么你有多余的main
這些運算符的調用和函數是一樣的 
他們有參數,也有返回類型,當然并不是所有的都是這樣,如 operator type()就沒有返回類型,其實type就是,調用我也不太清楚.如class operator+(),如是全局的則()內的一個是左操作數,一個是右操作數 ,class代表返回值,operator==類似

查看完整回答
反對 回復 2023-04-12
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

沒寫main函數唄

查看完整回答
反對 回復 2023-04-12
  • 2 回答
  • 0 關注
  • 166 瀏覽

添加回答

了解更多

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號