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

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在线| 亚洲人精品午夜| 久久精品成人| 国产午夜精品在线| 久久国产精品久久久久久| 一区二区国产日产| 欧美日韩国产在线看| 亚洲精品1区2区| 欧美va亚洲va国产综合| 久久婷婷麻豆| 亚洲精品日韩欧美| 91久久精品www人人做人人爽| 蜜桃久久精品乱码一区二区| 亚洲二区免费| 亚洲第一级黄色片| 欧美顶级少妇做爰| 一本色道久久综合狠狠躁篇怎么玩 | 欧美另类人妖| 99视频精品全国免费| 91久久国产综合久久蜜月精品 | 亚洲日本在线观看| 亚洲精品黄色| 欧美视频导航| 久久国产精品久久国产精品| 久久精品久久综合| 亚洲精选在线| 亚洲网站在线观看| 国内一区二区三区在线视频| 久久久久女教师免费一区| 久久婷婷综合激情| 99v久久综合狠狠综合久久| 99精品欧美一区| 国产一区二区三区精品久久久| 美日韩在线观看| 欧美日韩久久精品| 91久久香蕉国产日韩欧美9色| 欧美国产成人在线| 欧美日本不卡高清| 欧美亚洲综合久久| 裸体一区二区| 亚洲欧美一区二区三区极速播放| 欧美在线观看一区| 9人人澡人人爽人人精品| 亚洲性图久久| 亚洲欧洲三级电影| 午夜激情综合网| 99re在线精品| 久久久欧美一区二区| 亚洲视频一区| 久久野战av| 欧美一区二区三区婷婷月色| 久久婷婷影院| 久久国产精品久久国产精品| 欧美激情中文字幕在线| 久久久久久综合| 欧美日韩视频| 欧美韩日精品| 国产揄拍国内精品对白| 99热精品在线观看| 亚洲人成7777| 久久国产免费| 性色av一区二区三区| 欧美精品激情| 母乳一区在线观看| 国产亚洲精品aa午夜观看| 亚洲美女网站| a91a精品视频在线观看| 久久久免费av| 久久嫩草精品久久久精品一| 国产精品扒开腿做爽爽爽视频| 欧美激情第8页| 在线观看亚洲专区| 久久精品女人天堂| 久久精品国产精品亚洲综合| 国产精品久久久久久久午夜 | 欧美本精品男人aⅴ天堂| 久久久久九九九九| 国产精品一区二区久久国产| 日韩午夜电影av| 亚洲日韩成人| 女人香蕉久久**毛片精品| 另类专区欧美制服同性| 国产专区一区| 久久成人精品电影| 久久国产婷婷国产香蕉| 国产精品网站在线播放| 99re在线精品| 亚洲女同在线| 国产日韩欧美日韩| 欧美在线视频一区| 久久婷婷影院| 1024日韩| 欧美国产1区2区| 亚洲精选国产| 亚洲网址在线| 国产精品亚洲欧美| 欧美一区视频| 免费在线欧美视频| 亚洲激情视频在线| 欧美片网站免费| 亚洲视频中文| 久久国产精品久久w女人spa| 国产伪娘ts一区| 性一交一乱一区二区洋洋av| 亚洲欧美国产精品桃花| 国产精品一区免费观看| 欧美一区二区私人影院日本| 久久久一区二区| 最新中文字幕一区二区三区| 欧美另类视频在线| 亚洲欧美日韩精品在线| 久久久久久久成人| 亚洲国产另类久久久精品极度| 欧美激情1区2区3区| 中文精品99久久国产香蕉| 久久成人18免费网站| 亚洲高清一区二| 欧美激情一区二区久久久| 亚洲性夜色噜噜噜7777| 麻豆成人精品| 亚洲在线一区二区| 激情成人av在线| 欧美日韩午夜视频在线观看| 亚洲欧美成人精品| 欧美高清视频一区二区三区在线观看| 一本久久知道综合久久| 国产一在线精品一区在线观看| 欧美激情视频一区二区三区免费| 亚洲一区二区三区欧美| 欧美福利一区| 欧美在线视频在线播放完整版免费观看 | 女人天堂亚洲aⅴ在线观看| 日韩视频免费大全中文字幕| 国产精品久久久久91| 另类专区欧美制服同性| 亚洲无人区一区| 亚洲国产精品va在看黑人| 欧美在线观看天堂一区二区三区| 亚洲高清久久| 国产精品男人爽免费视频1| 女人色偷偷aa久久天堂| 欧美在线日韩在线| 亚洲男人的天堂在线观看| 亚洲国产精品一区制服丝袜| 久久久999成人| 亚洲欧美日韩国产综合在线| 亚洲三级影片| 在线成人欧美| 国产日韩欧美| 国产精品一卡二卡| 欧美三级不卡| 欧美精品日韩精品| 久久婷婷蜜乳一本欲蜜臀| 性久久久久久久| 亚洲欧美久久久| 宅男噜噜噜66国产日韩在线观看| 欧美v国产在线一区二区三区| 久久精品久久综合| 欧美一区二区三区免费观看| 亚洲免费中文字幕| 亚洲女女女同性video| 亚洲一区二区三区在线看| 一区二区三区四区在线| 一区二区日韩免费看| 一区二区福利| 在线视频日韩| 亚洲午夜高清视频| 在线视频欧美日韩| 亚洲午夜激情| 亚洲自拍偷拍福利| 亚洲欧美国产另类| 午夜精品久久久久久久白皮肤| 欧美成人综合一区| 欧美激情精品久久久久久大尺度| 性欧美长视频| 久久精品99| 久久综合图片| 免费在线看一区| 亚洲国产精品一区二区尤物区 | 午夜视频一区| 欧美一级久久久| 久久成人国产精品| 久久中文在线| 欧美激情在线狂野欧美精品| 欧美激情网友自拍| 欧美日韩三级在线| 国产精品亚洲综合色区韩国| 国产免费成人av| 国产在线精品成人一区二区三区| 狠狠色丁香久久婷婷综合_中| 伊人成年综合电影网| 亚洲人成在线观看网站高清| 在线中文字幕日韩| 久久精品二区亚洲w码|