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

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

類模板中的友元函數

Posted on 2013-01-21 10:48 魏尚堂 閱讀(615) 評論(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, 友元函數模板必須提前聲明


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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| 亚洲二区视频在线| 亚洲尤物影院| 国产精品视频久久| 欧美一区二区三区播放老司机| 在线视频你懂得一区| 国产精品毛片一区二区三区| 香蕉av福利精品导航| 亚洲欧美一区二区原创| 国产精品一区三区| 亚洲精品三级| 国产日韩精品一区二区三区| 国产欧美一区二区精品婷婷 | 暖暖成人免费视频| 亚洲日韩中文字幕在线播放| 亚洲精品久久久久久久久久久久| 欧美日本亚洲| 欧美中文日韩| 免费视频一区| 亚洲免费人成在线视频观看| 午夜精品久久久久久久久久久久久 | 久久成人这里只有精品| 亚洲成人直播| 亚洲午夜女主播在线直播| 国产亚洲一级| 亚洲美女色禁图| 国产手机视频一区二区| 欧美成熟视频| 国产精品欧美日韩一区二区| 蜜臀av性久久久久蜜臀aⅴ| 欧美激情综合在线| 久久久久久穴| 欧美日韩国产综合网| 久久天天躁狠狠躁夜夜爽蜜月| 欧美成人一区在线| 欧美在线999| 欧美日韩精品一区视频| 久久久久在线| 国产精品久久久亚洲一区| 欧美成人国产va精品日本一级| 欧美无乱码久久久免费午夜一区| 美女精品在线观看| 国产精品入口夜色视频大尺度| 欧美mv日韩mv国产网站| 国产美女精品视频免费观看| 亚洲日本中文字幕| 狠狠综合久久| 香蕉视频成人在线观看| 亚洲永久免费视频| 欧美久久久久久久久久| 久久综合久久综合九色| 国产精品午夜春色av| 99热在这里有精品免费| 亚洲电影av在线| 久久久久国产精品午夜一区| 亚洲欧美制服另类日韩| 欧美午夜电影完整版| 亚洲黄色精品| 日韩视频不卡| 欧美激情视频在线免费观看 欧美视频免费一| 久久国产精品久久久| 国产精品视频精品| 亚洲一区制服诱惑| 欧美一区二区免费| 国产精品免费在线| 亚洲夜间福利| 欧美一区二区三区精品电影| 欧美亚洲第一区| 亚洲视频在线一区| 亚洲一区二区动漫| 国产精品久久久久久久久久久久久 | 欧美一级夜夜爽| 国产精品高潮呻吟视频| 亚洲小视频在线观看| 午夜亚洲视频| 国产日韩一区二区| 欧美在线一二三四区| 久热精品在线视频| 在线观看欧美激情| 欧美a级一区| 日韩亚洲成人av在线| 亚洲欧美成aⅴ人在线观看| 国产精品激情电影| 亚洲欧美日韩一区在线| 久久精品综合| 亚洲激情啪啪| 欧美日韩精品伦理作品在线免费观看| 亚洲乱码一区二区| 久久成人精品| 91久久嫩草影院一区二区| 欧美二区在线观看| 一区二区三区欧美激情| 久久久久久久久蜜桃| 亚洲高清久久| 欧美视频一区二区在线观看| 亚洲免费视频观看| 你懂的国产精品永久在线| 日韩午夜电影av| 国产欧美日韩精品在线| 免费观看日韩av| 亚洲先锋成人| 欧美国产一区在线| 亚洲女人天堂成人av在线| 国产综合色产| 欧美精品99| 欧美一区二区大片| 亚洲日本在线视频观看| 欧美在线视频二区| 一区二区三区高清在线| 狠狠爱成人网| 国产精品久久久久久久久久免费| 欧美一区二区三区在线免费观看 | 欧美大片国产精品| 亚洲欧美日韩在线高清直播| 樱桃成人精品视频在线播放| 欧美日韩精品久久久| 久久久久久久久岛国免费| 99国产精品一区| 欧美激情亚洲视频| 久久九九电影| 亚洲制服少妇| 最新日韩av| 一区二区三区在线观看欧美| 国产精品区一区二区三区| 久久综合中文| 欧美中文在线字幕| 亚洲字幕在线观看| 99精品欧美一区二区三区综合在线| 另类专区欧美制服同性| 欧美一区二区三区视频| 一区二区三区视频观看| 亚洲国产激情| 伊人久久亚洲影院| 国内精品久久久久久久97牛牛| 国产精品欧美久久| 欧美三级电影一区| 欧美日韩国产大片| 另类欧美日韩国产在线| 久久精品欧美日韩精品| 欧美一区二区三区免费视| 亚洲制服av| 亚洲欧美一区二区原创| 亚洲四色影视在线观看| 在线视频你懂得一区| 一区二区三区视频在线播放| 亚洲乱码国产乱码精品精98午夜| 亚洲国产成人午夜在线一区 | 这里是久久伊人| 99re66热这里只有精品3直播| 亚洲国产另类 国产精品国产免费| 国产视频久久久久久久| 国产精品视频一区二区高潮| 国产精品久久久久久超碰| 国产精品国码视频| 国产美女精品一区二区三区| 国产视频精品xxxx| 狠狠色狠狠色综合| 91久久精品一区二区别| 亚洲精品在线观看免费| 一本一道久久综合狠狠老精东影业| 99国产精品| 亚洲一区二区久久| 午夜一区不卡| 久久久在线视频| 免费欧美在线视频| 亚洲三级视频在线观看| 99国产一区| 午夜免费日韩视频| 久久免费的精品国产v∧| 欧美顶级艳妇交换群宴| 国产精品99一区| 好吊日精品视频| 亚洲欧洲日产国产综合网| 一本久道久久久| 久久爱www| 欧美成人免费网站| 在线性视频日韩欧美| 欧美亚洲网站| 欧美成人午夜影院| 国产精品一二三视频| 在线观看欧美一区| 亚洲综合视频在线| 免费成人黄色| 亚洲自拍偷拍色片视频| 老司机精品视频一区二区三区| 欧美日韩高清一区| 国产一区在线播放| 亚洲少妇最新在线视频| 久久一区亚洲| 亚洲网站在线播放| 欧美福利影院| 伊人久久亚洲美女图片| 亚洲欧美久久| 亚洲国产二区| 久久久成人网| 国产精品丝袜久久久久久app| 亚洲国产精品电影| 久久激情视频免费观看| 日韩一区二区高清| 欧美成人日本|