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

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>
            国产精品久久久久久久9999| 久久精品成人| 午夜欧美理论片| 欧美韩日视频| 欧美与黑人午夜性猛交久久久| 狠狠做深爱婷婷久久综合一区| 欧美日韩精品一区二区三区| 一区二区三区日韩欧美精品| 国产亚洲欧美另类一区二区三区| 亚洲国产精品久久久| 一区久久精品| 欧美一级淫片aaaaaaa视频| 欧美在线观看一区| 亚洲第一二三四五区| 午夜国产精品视频| 欧美影院在线| 亚洲欧美成人综合| 一本色道久久精品| 经典三级久久| 国内精品久久久久久久影视蜜臀| 国产精品久久久久毛片软件| 亚洲制服av| 午夜精品一区二区三区在线播放| 香蕉精品999视频一区二区| 欧美影院成人| 欧美96在线丨欧| 99国产欧美久久久精品| 午夜欧美理论片| 欧美激情久久久久| 国产美女一区二区| 亚洲欧洲一区二区三区在线观看| 艳女tv在线观看国产一区| 久久se精品一区二区| 亚洲丶国产丶欧美一区二区三区| 亚洲麻豆一区| 久久久www免费人成黑人精品| 欧美激情视频在线播放 | 久久久久久国产精品一区| 欧美成人自拍视频| 亚洲欧美日本国产专区一区| 麻豆国产精品777777在线| 国产精品美女久久久久久久| 亚洲精品国产精品国自产在线| 亚洲欧美综合| 亚洲卡通欧美制服中文| 久久青草久久| 国产亚洲欧美日韩一区二区| 亚洲一区二区在线免费观看视频 | 99这里只有久久精品视频| 久久九九精品99国产精品| 午夜国产精品视频| 久久蜜桃精品| 欧美一区二区三区的| 欧美日韩一二三四五区| 国内外成人免费激情在线视频| 亚洲欧美日本日韩| 亚洲免费影视| 国产欧美二区| 午夜欧美大片免费观看 | 日韩一区二区免费看| 久久综合亚洲社区| 国精品一区二区| 久久美女性网| 久久久在线视频| 在线精品视频免费观看| 另类酷文…触手系列精品集v1小说| 午夜精品久久| 黄色日韩网站视频| 麻豆亚洲精品| 欧美sm极限捆绑bd| 99亚洲精品| 亚洲图片欧洲图片av| 欧美日韩国产二区| 亚洲欧美日韩精品久久久久| 亚洲男人的天堂在线| 国产一区二区三区在线观看免费视频 | 久久综合一区二区| 久久久亚洲成人| 亚洲片区在线| 99v久久综合狠狠综合久久| 国产精品福利av| 久久激情视频免费观看| 久久在精品线影院精品国产| 99re6热只有精品免费观看| 中文网丁香综合网| 国产一区二区三区高清在线观看 | 免费亚洲电影在线观看| 99热免费精品在线观看| 亚洲性视频h| 韩国一区电影| 亚洲激情国产精品| 国产精品久久97| 老司机一区二区| 欧美日韩精品免费看| 欧美一区二区日韩| 另类春色校园亚洲| 亚洲女爱视频在线| 久久综合中文| 亚洲欧美激情一区| 久久精品国产综合| 欧美一级久久久久久久大片| 欧美第十八页| 亚洲国产毛片完整版| 亚洲国产精品欧美一二99| 久久久999| 久久夜色撩人精品| 久久av二区| 国产伦精品一区二区三区| 午夜亚洲精品| 1024成人| 亚洲一区bb| 日韩午夜精品| 久久久久.com| 性高湖久久久久久久久| 久热精品在线| 久久精品国产亚洲一区二区三区| 欧美激情无毛| 女女同性精品视频| 国产欧美一区二区三区另类精品 | 国产精品主播| 日韩一级裸体免费视频| 亚洲第一精品影视| 欧美一区二区三区四区夜夜大片| 一本久久综合亚洲鲁鲁五月天| 久久免费视频网站| 久久精品二区亚洲w码| 国产精品久久激情| 一区二区精品国产| 99亚洲伊人久久精品影院红桃| 老司机aⅴ在线精品导航| 久久综合九色综合欧美狠狠| 国产一区二区福利| 午夜一区二区三区在线观看| 亚洲欧美精品在线观看| 欧美视频在线视频| 一区二区激情| 欧美一区二区视频在线观看2020| 国产精品二区在线| 一本色道**综合亚洲精品蜜桃冫| 日韩视频精品在线| 欧美精品系列| 日韩一级免费| 午夜在线视频一区二区区别| 国产精品国产三级欧美二区| 一本久道久久久| 亚洲女性裸体视频| 国产欧美一区二区三区另类精品| 亚洲欧美日韩一区在线观看| 欧美亚洲色图校园春色| 国产欧美精品一区二区三区介绍| 亚洲欧美激情在线视频| 欧美在线亚洲在线| 黄色亚洲免费| 欧美成人精品福利| 亚洲一区视频在线| 久久成人免费| 欧美va天堂在线| av72成人在线| 欧美专区在线| 欧美日本韩国一区| 国产精品视屏| 欧美精品国产一区| 尤物九九久久国产精品的特点| 国产精品一区二区你懂得| 久久爱另类一区二区小说| 欧美凹凸一区二区三区视频| 欧美高清视频在线| 亚洲永久在线观看| 国内外成人免费视频| 免费试看一区| 亚洲欧美不卡| 欧美高清视频一区| 亚洲免费一在线| 悠悠资源网亚洲青| 欧美小视频在线| 久久青草福利网站| 亚洲一区3d动漫同人无遮挡| 欧美国产日韩精品| 午夜视频一区| 亚洲三级网站| 国产美女精品| 欧美精品在线免费播放| 午夜精品视频一区| 亚洲人成网站精品片在线观看| 欧美在线视频不卡| 一区二区激情视频| 在线色欧美三级视频| 国产精品一区二区你懂得| 欧美国产精品久久| 久久福利一区| 亚洲图片欧洲图片av| 亚洲精品资源美女情侣酒店| 久久久久久久欧美精品| 亚洲欧美日韩中文播放| 日韩一区二区免费看| 尤物九九久久国产精品的特点| 国产精品人人做人人爽人人添| 欧美巨乳波霸| 欧美成人国产一区二区| 久久国产主播|