青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 9, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

類模板中的友元函數

Posted on 2013-01-21 10:48 魏尚堂 閱讀(607) 評論(0)  編輯 收藏 引用
template <typename T> class dataList
{
   public:
      friend ostream& operator<<(ostream& outStream, const dataList <T> &outList);
}
template <typename T> ostream& operator<<(ostream& outStream, const dataList <T> &outList)
{
   //....
   return outStream;
}
int main(int argc, char* argv[])
{
   dataList <int> testList;
   cout << testList;
}
這個程序員是鏈接不過,
錯誤信息:
 warning: friend declaration âstd::ostream& operator<<(std::ostream&, const dataList<T>&)â declares a non-template function
 note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
/tmp/cc9DSuka.o: In function `main':
undefined reference to `operator<<(std::basic_ostream<char, std::char_traits<char> >&, dataList<int> const&)'
collect2: ld returned 1 exit status
錯誤原因解釋

The problem is that the compiler is not trying to use the templated operator<< you provided, but rather a non-templated version.

When you declare a friend inside a class you are injecting the declaration of that function in the enclosing scope. The following code has the effect of declaring (and not defining) a free function that takes a non_template_test argument by constant reference:

class non_template_test { friend void f( non_template_test const & ); }; // declares here: // void f( non_template_test const & );

The same happens with template classes, even if in this case it is a little less intuitive. When you declare (and not define) a friend function within the template class body, you are declaring a free function with that exact arguments. Note that you are declaring a function, not a template function:

template<typename T> class template_test { friend void f( template_test<T> const & t ); }; // for each instantiating type T (int, double...) declares: // void f( template_test<int> const & ); // void f( template_test<double> const & );  int main() {     template_test<int> t1;     template_test<double> t2; }

Those free functions are declared but not defined. The tricky part here is that those free functions are not a template, but regular free functions being declared. When you add the template function into the mix you get:

template<typename T> class template_test { friend void f( template_test<T> const & ); }; // when instantiated with int, implicitly declares: // void f( template_test<int> const & );  template <typename T> void f( template_test<T> const & x ) {} // 1  int main() {    template_test<int> t1;    f( t1 ); }

When the compiler hits the main function it instantiates the template template_test with type intand that declares the free function void f( template_test<int> const & ) that is not templated. When it finds the call f( t1 ) there are two f symbols that match: the non-template f( template_test<int> const & ) declared (and not defined) when template_test was instantiated and the templated version that is both declared and defined at 1. The non-templated version takes precedence and the compiler matches it.

When the linker tries to resolve the non-templated version of f it cannot find the symbol and it thus fails.

What can we do? There are two different solutions. In the first case we make the compiler provide non-templated functions for each instantiating type. In the second case we declare the templated version as a friend. They are subtly different, but in most cases equivalent.

Having the compiler generate the non-templated functions for us:

template <typename T> class test  { friend void f( test<T> const & ) {} }; // implicitly

This has the effect of creating as many non-templated free functions as needed. When the compiler finds the friend declaration within the template test it not only finds the declaration but also the implementation and adds both to the enclosing scope.

Making the templated version a friend

To make the template a friend we must have it already declared and tell the compiler that the friend we want is actually a template and not a non-templated free function:

template <typename T> class test; // forward declare the template class template <typename T> void f( test<T> const& ); // forward declare the template template <typename T> class test { friend void f<>( test<T> const& ); // declare f<T>( test<T> const &) a friend }; template <typename T> void f( test<T> const & ) {}

In this case, prior to declaring f as a template we must forward declare the template. To declare the ftemplate we must first forward declare the test template. The friend declaration is modified to include the angle brackets that identify that the element we are making a friend is actually a template and not a free function.
引用自 http://stackoverflow.com/questions/1810753/overloading-operator-for-a-templated-class
從上面我可以學到一點:
1, 編譯器匹配方法時非模板函數優先模板函數
2, 友元函數模板必須提前聲明

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲欧洲精品一区二区三区 | 亚洲一区二区三区色| 国产中文一区二区| 一区二区三区在线不卡| 欧美激情日韩| 欧美日本韩国| 国产精品久久久久久久久久久久久久 | 欧美日韩亚洲在线| 欧美网站在线| 国产精品爽爽爽| 狠狠爱综合网| 亚洲精品一级| 欧美亚洲一区二区三区| 久久婷婷一区| 亚洲精品国产视频| 午夜一区不卡| 欧美不卡一卡二卡免费版| 欧美日韩国产黄| 国产午夜精品视频| 亚洲欧洲一级| 久久成人免费日本黄色| 欧美黄色一区二区| 亚洲一区二区黄色| 另类天堂av| 国产精品外国| 亚洲精品亚洲人成人网| 久久精品亚洲热| 日韩视频一区二区三区在线播放免费观看 | 一区二区三区国产盗摄| 欧美一区1区三区3区公司| 久久精品二区三区| 欧美午夜电影在线| 亚洲电影在线看| 亚洲欧美在线免费| 亚洲高清激情| 久久av红桃一区二区小说| 欧美日韩国产色视频| 黄色成人av在线| 亚洲欧美韩国| 亚洲精品免费在线| 午夜精品久久久久久久蜜桃app | 亚洲欧洲精品一区二区| 午夜在线a亚洲v天堂网2018| 欧美午夜精品理论片a级按摩| 欧美日韩一区二区三区在线看| 国产精品视频免费| 99av国产精品欲麻豆| 久久综合网hezyo| 午夜精品一区二区三区在线视| 欧美日韩一区综合| 一本色道久久综合| 欧美风情在线观看| 久久久久网站| 亚洲人成亚洲人成在线观看| 久久综合色一综合色88| 国产亚洲精品高潮| 欧美在线国产精品| 亚洲欧美视频一区二区三区| 国产精品自拍三区| 欧美怡红院视频| 午夜精品区一区二区三| 国产精品免费一区豆花| 亚洲欧美bt| 亚洲一区二区三区中文字幕在线| 国产精品久久一区二区三区| 欧美一区二区三区视频| 亚洲字幕在线观看| 国产婷婷色一区二区三区四区| 欧美一区二区三区四区视频 | 日韩视频免费观看高清在线视频| 欧美精品久久天天躁| 亚洲美女在线一区| 亚洲欧洲精品一区二区三区 | 欧美成熟视频| 999在线观看精品免费不卡网站| 亚洲精品乱码久久久久久按摩观 | 欧美视频三区在线播放| 亚洲女同在线| 欧美在线日韩| 亚洲日本欧美日韩高观看| 91久久精品国产91久久性色| 欧美日韩一区二区精品| 久久国产主播| 开元免费观看欧美电视剧网站| 亚洲欧洲日韩在线| 一区二区精品| 激情伊人五月天久久综合| 亚洲国产99| 国产精品区一区二区三区| 浪潮色综合久久天堂| 欧美久久久久久久| 欧美一区激情| 欧美国内亚洲| 久久精品国产精品亚洲精品| 免费成人你懂的| 欧美伊久线香蕉线新在线| 美国成人毛片| 久久高清福利视频| 久久精品国产77777蜜臀| 一区二区三区精品| 国产亚洲一区在线| 亚洲区一区二区三区| 国产欧美日韩高清| 亚洲韩国精品一区| 国产欧美日韩另类视频免费观看| 亚洲福利视频网| 国产精品视频精品| 亚洲国产欧美另类丝袜| 国产精品一二三| 亚洲国产精品成人精品| 国产欧美一区二区精品婷婷| 亚洲人人精品| 1769国产精品| 午夜亚洲一区| 亚洲一区二区动漫| 欧美mv日韩mv国产网站app| 性色一区二区| 欧美视频四区| 99精品国产在热久久| 亚洲国产欧洲综合997久久| 欧美中文日韩| 久久av一区二区三区亚洲| 欧美日韩日日夜夜| 欧美激情成人在线视频| 国内精品视频久久| 欧美专区福利在线| 久久国产精品网站| 国产精品一卡| 午夜精品理论片| 欧美诱惑福利视频| 国产精品xxxxx| 亚洲日韩成人| 一区二区三区国产盗摄| 欧美高清一区二区| 亚洲第一区在线观看| 在线看成人片| 免费观看一区| 亚洲丰满在线| 亚洲美女视频在线观看| 欧美福利视频一区| 亚洲国产婷婷香蕉久久久久久99| 亚洲国产视频直播| 免费看亚洲片| 亚洲成色777777女色窝| 亚洲精品乱码久久久久久久久 | 欧美体内谢she精2性欧美| 亚洲激情专区| 亚洲最新视频在线播放| 欧美日韩国产成人高清视频| 一区二区高清视频| 欧美与欧洲交xxxx免费观看| 国产一区二区三区在线观看网站 | 久久久xxx| 欧美国产日产韩国视频| 亚洲精品欧美日韩专区| 欧美日韩免费区域视频在线观看| 99精品欧美一区| 午夜精品久久久久久久99黑人| 国产婷婷精品| 欧美成人高清| 亚洲美女啪啪| 欧美尤物巨大精品爽| 亚洲大片av| 欧美丝袜一区二区| 欧美在线视频一区二区三区| 亚洲第一在线| 亚洲欧美在线aaa| 亚洲电影在线免费观看| 欧美三区在线观看| 欧美一区二区成人| 欧美激情久久久久久| 亚洲无限av看| 红桃av永久久久| 国产精品sm| 久久天天躁夜夜躁狠狠躁2022 | 久久国产精品一区二区| 欧美激情乱人伦| 久久国产色av| 99伊人成综合| 在线精品视频一区二区| 国产精品高清网站| 噜噜噜91成人网| 亚洲制服av| 99国产精品久久久久久久| 久热精品在线视频| 性伦欧美刺激片在线观看| 亚洲精品美女久久7777777| 国产精品一区亚洲| 欧美精品在线一区二区| 久久久精品一区| 亚洲一区一卡| 一本久久a久久免费精品不卡| 欧美插天视频在线播放| 亚洲欧美偷拍卡通变态| 日韩视频精品在线观看| 亚洲国产黄色片| 极品少妇一区二区三区精品视频| 国产精品欧美日韩久久| 欧美日韩一区二区三区在线视频| 欧美激情一区二区三区在线视频观看|