第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

重載朋友操作符<for Template類

重載朋友操作符<for Template類

C++
躍然一笑 2019-06-18 11:21:20
重載朋友操作符<for Template類現(xiàn)在,我在stackoverflow網(wǎng)站上讀到了幾個關(guān)于我的問題,但這些問題似乎都解決不了我的問題?;蛘呶易鲥e了.超載<<如果我把它變成內(nèi)聯(lián)函數(shù)就行了。但我怎樣才能讓它在我的案子里起作用呢?warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template functionwarning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning/tmp/cc6VTWdv.o:uppgift4.cc:(.text+0x180): undefined reference to operator<<(std::basic_ostream<char, std::char_traits<char> >&, D<int> const&)'  collect2: ld returned 1 exit status守則:template <class T>T my_max(T a, T b){    if(a > b)             return a;    else       return b;}template <class classT>class D{public:    D(classT in)       : d(in) {};    bool operator>(const D& rhs) const;    classT operator=(const D<classT>& rhs);    friend ostream& operator<< (ostream & os, const D<classT>& rhs);private:    classT d;};int main(){    int i1 = 1;    int i2 = 2;    D<int> d1(i1);    D<int> d2(i2);    cout << my_max(d1,d2) << endl;    return 0;}template <class classT>ostream& operator<<(ostream &os, const D<classT>& rhs){    os << rhs.d;    return os;}
查看完整描述

3 回答

?
蝴蝶不菲

TA貢獻1810條經(jīng)驗 獲得超4個贊

您不能這樣聲明一個朋友,您需要為它指定一個不同的模板類型。

template <typename SclassT>friend ostream& operator<< (ostream & os, const D<SclassT>& rhs);

SclassT這樣它就不會蒙上陰影classT..定義時

template <typename SclassT>ostream& operator<< (ostream & os, const D<SclassT>& rhs){
  // body..}


查看完整回答
反對 回復(fù) 2019-06-18
?
胡說叔叔

TA貢獻1804條經(jīng)驗 獲得超8個贊

在沒有任何編譯器警告的情況下,這對我有效。

#include <iostream>using namespace std;template <class T>T my_max(T a, T b){
  if(a > b)
    return a;
  else
    return b;}template <class classT>class D{public:
  D(classT in)
    : d(in) {};

  bool operator>(const D& rhs) const {
    return (d > rhs.d);
  }

  classT operator=(const D<classT>& rhs);

  friend ostream& operator<< (ostream & os, const D& rhs) {
    os << rhs.d;
    return os;
  }private:
  classT d;};int main(){

  int i1 = 1;
  int i2 = 2;
  D<int> d1(i1);
  D<int> d2(i2);

  cout << my_max(d1,d2) << endl;
  return 0;}


查看完整回答
反對 回復(fù) 2019-06-18
  • 3 回答
  • 0 關(guān)注
  • 441 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號