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

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>
            亚洲高清成人| 免费成人在线视频网站| 欧美黄污视频| 另类尿喷潮videofree| 在线观看亚洲精品| 久久综合伊人| 欧美大片在线影院| 9l国产精品久久久久麻豆| 亚洲精选一区| 国产日韩欧美二区| 美女黄网久久| 欧美激情精品久久久久久蜜臀| 一本大道久久a久久综合婷婷| 99热在线精品观看| 国产婷婷精品| 欧美激情第1页| 欧美午夜一区二区三区免费大片 | 国产精品视频免费观看| 久久国产精品第一页| 另类春色校园亚洲| 亚洲一区二区三区中文字幕| 午夜精品一区二区三区在线| 1000部国产精品成人观看| 亚洲精品中文字幕在线| 国产女主播在线一区二区| 蜜桃久久av| 国产精品你懂的| 欧美成人一品| 国产精品亚洲欧美| 欧美激情1区| 国产农村妇女精品| 亚洲国产精品传媒在线观看| 国产精品国产成人国产三级| 免费一区视频| 国产精品一卡| 日韩视频在线观看免费| 国产无一区二区| 亚洲精品一区二区三区在线观看 | 欧美精品97| 久久性色av| 国产日韩欧美亚洲一区| 亚洲肉体裸体xxxx137| 韩国av一区二区三区在线观看| 欧美黑人在线播放| 国语自产精品视频在线看8查询8| 日韩午夜在线| 亚洲精品久久久一区二区三区| 午夜精品久久久久久久| 亚洲视频综合在线| 欧美激情视频网站| 欧美高清视频在线播放| 国产综合色精品一区二区三区| 99精品欧美一区二区三区综合在线| 激情五月婷婷综合| 久久av一区二区三区漫画| 欧美一区二区大片| 国产精品视频网址| 在线亚洲精品福利网址导航| 99av国产精品欲麻豆| 免费人成网站在线观看欧美高清 | 一本久道久久综合婷婷鲸鱼| 久久免费精品日本久久中文字幕| 欧美一区二区三区在线观看视频| 欧美午夜激情视频| 一级日韩一区在线观看| 夜久久久久久| 欧美日韩国产成人在线91| 亚洲国产日韩精品| 99精品国产福利在线观看免费| 欧美成人激情在线| 亚洲精选在线| 亚洲欧美日韩国产综合在线 | 欧美一区中文字幕| 久久久久久999| 精品二区久久| 欧美成年人视频网站| 亚洲国产黄色片| 一区二区欧美激情| 欧美系列精品| 午夜精品福利视频| 麻豆视频一区二区| 99精品国产在热久久| 国产精品a久久久久| 国产精品99久久久久久久久久久久| 亚洲一区中文| 国产在线一区二区三区四区| 久久久久网址| 亚洲欧洲日夜超级视频| 亚洲无线一线二线三线区别av| 国产精品久久一区二区三区| 欧美亚洲一级| 亚洲第一区在线观看| 在线亚洲电影| 国产一二三精品| 欧美成年人网| 亚洲欧美视频| 亚洲黄色大片| 久久国产精品免费一区| 亚洲国产日韩欧美一区二区三区| 欧美激情91| 欧美一区二区成人| 91久久国产自产拍夜夜嗨| 亚洲女ⅴideoshd黑人| 尤物99国产成人精品视频| 欧美精品一线| 久久久青草青青国产亚洲免观| 亚洲裸体在线观看| 麻豆精品国产91久久久久久| 日韩午夜电影在线观看| 国产一区观看| 国产精品高潮呻吟久久| 久久中文字幕一区| 午夜日韩av| 日韩视频一区二区三区在线播放| 欧美在线免费观看视频| 99国产精品自拍| 亚洲第一福利社区| 国产日韩欧美精品综合| 欧美日韩小视频| 免费亚洲一区二区| 欧美在线一二三| 亚洲一区二区三区色| 亚洲国产精品久久久久久女王| 久久精品国产一区二区三区| 一区二区三区国产| 亚洲精品美女在线观看| 狠狠色综合网站久久久久久久| 欧美视频四区| 欧美黑人在线观看| 暖暖成人免费视频| 久久视频国产精品免费视频在线| 亚洲图片你懂的| 99re成人精品视频| 亚洲日本国产| 亚洲国产精品一区二区www在线| 久久噜噜噜精品国产亚洲综合| 亚洲免费影视| 午夜老司机精品| 亚洲欧美日韩在线综合| 国产精品99久久不卡二区| 亚洲精品国产欧美| 亚洲欧洲日产国产网站| 亚洲经典在线| 亚洲乱码国产乱码精品精可以看| 亚洲高清资源| 亚洲每日更新| 一本大道av伊人久久综合| 一区二区三区 在线观看视频| 亚洲美女av网站| av成人国产| 亚洲在线成人| 欧美一级久久| 玖玖玖国产精品| 欧美激情乱人伦| 亚洲美女免费精品视频在线观看| 亚洲毛片一区| 亚洲欧美日韩综合国产aⅴ | 免费在线观看成人av| 免费不卡在线观看| 欧美国产综合视频| 日韩一级成人av| 午夜国产精品影院在线观看| 久久成人综合网| 欧美刺激性大交免费视频| 欧美精品性视频| 国产精品一区二区三区四区五区| 国产日韩欧美精品综合| 在线视频国产日韩| 亚洲免费综合| 开元免费观看欧美电视剧网站| 欧美激情第4页| 亚洲一级在线| 久久精品综合网| 欧美精品一区二区在线观看| 欧美性视频网站| 在线色欧美三级视频| 亚洲无线观看| 美女国产一区| 一区二区三区视频在线| 久久免费一区| 国产精品成人久久久久| 影音先锋亚洲一区| 亚洲香蕉在线观看| 免费一级欧美片在线观看| 亚洲深夜福利在线| 免费观看一区| 国产日韩精品视频一区| 亚洲精品乱码| 久久躁日日躁aaaaxxxx| 日韩亚洲国产欧美| 老司机精品视频网站| 国产精品亚洲片夜色在线| 最新亚洲电影| 蘑菇福利视频一区播放| 亚洲一级一区| 欧美天天综合网| 亚洲麻豆av| 欧美成人精品在线| 欧美一区午夜精品| 国产精品日韩欧美一区|