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

posts - 9, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
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;
}
這個(gè)程序員是鏈接不過,
錯(cuò)誤信息:
 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
錯(cuò)誤原因解釋

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
從上面我可以學(xué)到一點(diǎn):
1, 編譯器匹配方法時(shí)非模板函數(shù)優(yōu)先模板函數(shù)
2, 友元函數(shù)模板必須提前聲明


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   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网站网址麻豆| 国产手机视频一区二区| 欧美亚洲自偷自偷| 美女91精品| 亚洲欧洲日本专区| 欧美日韩午夜在线| 亚洲中无吗在线| 看片网站欧美日韩| 99热这里只有精品8| 国产精品日韩欧美| 久久亚洲综合色一区二区三区| 欧美国产大片| 西瓜成人精品人成网站| 一区二区三区在线观看视频| 欧美—级在线免费片| 一本在线高清不卡dvd | 亚洲深爱激情| 国产视频久久久久| 欧美精品电影| 香蕉久久夜色| 亚洲人成人一区二区三区| 欧美一级网站| 亚洲老板91色精品久久| 国产精品永久| 欧美成人一区二区在线| 亚洲欧美999| 亚洲人成网站在线播| 欧美在线精品免播放器视频| 亚洲欧洲日韩在线| 国产亚洲二区| 欧美日韩精品一区二区天天拍小说| 午夜视频久久久| 日韩一区二区免费高清| 欧美1区视频| 久久精品国产久精国产思思| 亚洲最新合集| 亚洲国产精品va在线看黑人| 国产伦精品一区二区三区免费迷| 欧美成人激情在线| 欧美在线国产精品| 亚洲无吗在线| 亚洲精品中文字幕在线| 欧美成人精品1314www| 欧美一区综合| 亚洲中午字幕| 中文亚洲欧美| 亚洲欧洲在线视频| 在线视频观看日韩| 国产亚洲一二三区| 国产精品综合不卡av| 欧美午夜在线观看| 欧美日韩国产限制| 欧美岛国在线观看| 另类av一区二区| 久久先锋影音| 久久久久久电影| 欧美中文在线免费| 久久riav二区三区| 亚洲欧美久久久久一区二区三区| 99精品视频免费全部在线| 亚洲国产精品一区| 欧美激情中文不卡| 欧美国产精品中文字幕| 嫩草影视亚洲| 欧美激情一区二区三区全黄| 免费一级欧美片在线播放| 久久理论片午夜琪琪电影网| 久久精品一区二区国产| 久久久免费精品| 久久三级福利| 女人天堂亚洲aⅴ在线观看| 美女亚洲精品| 亚洲电影免费观看高清完整版在线观看 | 午夜精品久久久久久99热软件| 一二美女精品欧洲| 一区二区免费在线观看| 亚洲视频专区在线| 亚洲欧美激情诱惑| 久久精品国产免费观看| 久久久久久久综合色一本| 久久视频在线视频| 欧美成人第一页| 欧美日韩免费一区二区三区| 欧美日韩性生活视频| 国产精品久久久久久久久久久久| 国产精品国产三级国产普通话蜜臀 | 欧美国产大片| 亚洲精品一区二区三区不| 亚洲精品在线免费| 亚洲一级二级在线| 久久精品99| 久久综合久久综合久久综合| 欧美成人一二三| 欧美日韩一区二区在线播放| 国产精品久久久免费| 国产一区亚洲| 亚洲精品日韩久久| 午夜精品久久久| 久久这里只有| 亚洲毛片在线免费观看| 亚洲免费在线视频| 久久久久久色| 欧美午夜激情在线| 激情成人综合网| 99在线热播精品免费99热| 性欧美在线看片a免费观看| 麻豆亚洲精品| 99精品久久久| 久久成人精品| 欧美日韩国产精品一区| 国产视频精品免费播放| 亚洲卡通欧美制服中文| 欧美一级在线视频| 亚洲国产精品高清久久久| 亚洲在线视频免费观看| 久久综合九色综合欧美狠狠| 欧美三级不卡| **欧美日韩vr在线| 午夜精品久久久久久久| 亚洲成人在线网站| 亚洲欧美激情四射在线日 | 在线欧美不卡| 午夜精品亚洲一区二区三区嫩草| 欧美a级片一区| 亚洲女同精品视频| 欧美日本中文字幕| 亚洲第一福利社区| 性久久久久久| 99在线精品视频| 麻豆国产精品一区二区三区| 国产精品视频区| 中文久久精品| 亚洲电影专区| 久久噜噜噜精品国产亚洲综合| 国产精品成人一区| 99视频一区二区| 欧美激情精品久久久久久黑人| 欧美亚洲在线播放| 国产精品一二三| 亚洲深夜福利在线| 亚洲欧洲日本一区二区三区| 久久噜噜亚洲综合| 国内一区二区三区在线视频| 欧美一区二区三区视频在线 | 99re国产精品| 欧美国产日韩一区二区| 在线看无码的免费网站| 久久精品av麻豆的观看方式| 亚洲一区bb| 国产精品久久久久99| 亚洲午夜视频在线| 亚洲精品影院| 欧美人与禽性xxxxx杂性| 亚洲精品欧美一区二区三区| 欧美波霸影院| 麻豆精品视频在线| 亚洲区国产区| 91久久精品日日躁夜夜躁国产| 蜜臀av性久久久久蜜臀aⅴ四虎| 在线观看中文字幕不卡| 免费国产一区二区| 免费成人性网站| 亚洲人成7777| 亚洲欧洲精品一区二区三区波多野1战4 | 亚洲天堂av在线免费观看| 亚洲人线精品午夜| 欧美日韩在线视频首页| 亚洲午夜女主播在线直播| 国产精品99久久久久久久vr| 欧美性做爰毛片| 亚洲欧美日韩综合国产aⅴ| 亚洲夜间福利| 国产一区二区三区四区三区四 | av成人天堂| 在线视频精品| 国产欧美日韩伦理| 久久综合一区二区三区| 久久久激情视频| 亚洲韩国一区二区三区| 91久久国产综合久久| 欧美日韩综合在线| 久久国产综合精品| 麻豆精品国产91久久久久久| 一本色道久久综合亚洲精品小说| 一区二区三区日韩精品| 国产日韩欧美高清| 欧美黄色精品| 国产精品草莓在线免费观看| 久久久7777| 欧美高清影院| 欧美尤物一区| 欧美成人免费在线观看| 亚洲欧美日韩在线观看a三区| 久久精品99国产精品酒店日本| 亚洲欧洲日韩女同| 亚洲综合国产激情另类一区| 尤物在线精品| 亚洲无亚洲人成网站77777| 激情欧美一区二区三区在线观看|