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

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

類模板中的友元函數

Posted on 2013-01-21 10:48 魏尚堂 閱讀(604) 評論(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成人激情| 国产一区二区三区成人欧美日韩在线观看 | 免费91麻豆精品国产自产在线观看| 欧美一区二区三区视频在线观看| 国产一级一区二区| 老牛嫩草一区二区三区日本| 欧美高清在线| 欧美一区二区三区成人| 久久久久久久综合色一本| 亚洲精品视频在线观看免费| 一区二区三区成人精品| 黄色一区三区| 在线视频精品一区| 在线看欧美视频| 在线视频免费在线观看一区二区| 国产亚洲在线观看| 亚洲裸体俱乐部裸体舞表演av| 国产精品入口尤物| 欧美高清视频一区| 国产视频一区二区在线观看| 亚洲人成久久| 国产人久久人人人人爽| 亚洲国产中文字幕在线观看| 国产欧美日韩亚洲一区二区三区| 欧美99在线视频观看| 国产精品亚洲综合天堂夜夜 | 中文在线不卡视频| 久久久久91| 午夜视频久久久| 欧美国产极速在线| 美国十次了思思久久精品导航| 欧美日韩中文字幕| 欧美高清一区| 影音先锋中文字幕一区| 亚洲伊人伊色伊影伊综合网| 一本色道久久88精品综合| 久久精品亚洲精品国产欧美kt∨| 亚洲一区二区免费| 欧美喷潮久久久xxxxx| 久久亚洲综合色一区二区三区| 欧美日韩亚洲高清| 亚洲国产福利在线| 91久久久久久久久| 久久亚洲一区| 欧美成人免费全部观看天天性色| 国产欧美精品日韩| 亚洲一线二线三线久久久| 亚洲天堂av图片| 欧美区亚洲区| 亚洲高清在线精品| 亚洲国产午夜| 免费亚洲电影在线观看| 欧美激情第4页| 激情亚洲网站| 久久亚洲综合网| 欧美粗暴jizz性欧美20| 在线观看日韩一区| 久久久噜噜噜久久中文字免| 美日韩精品视频| 亚洲国产欧美在线| 免费人成网站在线观看欧美高清| 免费久久久一本精品久久区| 亚洲福利精品| 免费观看欧美在线视频的网站| 欧美阿v一级看视频| 1769国产精品| 欧美成人黄色小视频| 亚洲国产精品一区二区久| 亚洲国产精品电影| 欧美理论电影在线观看| 中文在线资源观看网站视频免费不卡 | 久久九九免费| 国内精品久久久久久久影视麻豆| 亚洲图中文字幕| 久久久www成人免费精品| 亚洲福利国产| 欧美日韩1区2区| 亚洲在线观看视频网站| 久久亚洲春色中文字幕| 亚洲精品视频啊美女在线直播| 欧美日韩成人综合在线一区二区| 一区二区欧美视频| 久久久精品2019中文字幕神马| 在线国产欧美| 欧美日韩在线精品| 亚洲欧美日产图| 免费成人你懂的| 亚洲一区二区视频在线| 国产午夜精品福利| 欧美久久久久久久久久| 亚洲在线视频观看| 女仆av观看一区| 午夜精品久久久久影视| 亚洲国产成人91精品| 欧美午夜精品久久久久免费视| 久久精品人人爽| 一本色道久久加勒比88综合| 久久综合给合久久狠狠狠97色69| 日韩视频精品在线| 国产综合在线视频| 欧美日韩日日骚| 久久久国产一区二区三区| 亚洲精品中文字| 欧美刺激午夜性久久久久久久| 亚洲欧美日韩成人| 亚洲激情视频| 国产一区二区三区av电影| 欧美三级午夜理伦三级中视频| 久久日韩粉嫩一区二区三区| 亚洲男人影院| 日韩一区二区免费高清| 欧美激情国产日韩精品一区18| 性久久久久久久久| 在线亚洲自拍| 亚洲精品日日夜夜| 影音先锋国产精品| 国产日韩欧美精品一区| 国产精品美女xx| 欧美日韩亚洲视频| 欧美黑人多人双交| 免费高清在线视频一区·| 欧美自拍偷拍午夜视频| 亚洲欧美区自拍先锋| 一区二区欧美国产| 一区二区三区成人精品| 9i看片成人免费高清| 91久久久久久国产精品| 欧美国产免费| 亚洲国产欧美精品| 亚洲日本一区二区三区| 亚洲国产精品精华液2区45| 你懂的视频一区二区| 免费国产一区二区| 欧美成人国产va精品日本一级| 久久婷婷国产综合尤物精品 | 一区二区三区免费观看| 99re在线精品| 一区二区三区日韩精品| 亚洲午夜极品| 亚洲一区视频| 欧美一区综合| 久久色在线播放| 欧美69wwwcom| 91久久嫩草影院一区二区| 亚洲肉体裸体xxxx137| 日韩一二在线观看| 一区二区av在线| 午夜老司机精品| 久久精品综合一区| 欧美va亚洲va国产综合| 欧美裸体一区二区三区| 国产精品久久久久久久久久ktv| 欧美三级在线视频| 国产日韩精品一区二区三区在线| 国产亚洲欧美激情| 亚洲精品久久嫩草网站秘色| 99热这里只有精品8| 欧美一级片在线播放| 久久久久天天天天| 亚洲国产免费| 午夜精品视频一区| 久久在线免费视频| 欧美日韩一级视频| 国产一区二区三区在线观看精品 | 亚洲国产美女精品久久久久∴| 夜夜精品视频| 久久久久91| 亚洲精品一区二区三区樱花| 亚洲视频免费观看| 久久综合久久久久88| 欧美激情精品久久久| 国产精品亚洲综合天堂夜夜| 亚洲大胆人体视频| 亚洲免费视频中文字幕| 久久综合九色99| 一区二区激情视频| 午夜视频一区| 欧美日韩天堂| 影院欧美亚洲| 欧美在线观看视频一区二区| 欧美成人一区二区在线| 亚洲视频一二区| 欧美jizz19性欧美| 国产日韩欧美在线观看| 一区二区激情小说| 开心色5月久久精品| 夜久久久久久| 欧美精品在线一区| 一区二区自拍| 久久精品亚洲精品| 在线视频欧美日韩精品| 欧美大片专区| 在线视频观看日韩| 久久高清国产|