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

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

類模板中的友元函數(shù)

Posted on 2013-01-21 10:48 魏尚堂 閱讀(607) 評論(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
從上面我可以學(xué)到一點:
1, 編譯器匹配方法時非模板函數(shù)優(yōu)先模板函數(shù)
2, 友元函數(shù)模板必須提前聲明


只有注冊用戶登錄后才能發(fā)表評論。
網(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>
            国产精品99久久久久久久vr| 欧美日韩一区二区三区四区五区| 欧美不卡三区| 欧美自拍丝袜亚洲| 久久久久久久久久久一区| 欧美制服丝袜| 另类激情亚洲| 亚洲国产成人久久综合一区| 麻豆精品国产91久久久久久| 亚洲第一页在线| 亚洲理伦电影| 亚洲免费小视频| 欧美一区二区三区四区夜夜大片| 久久日韩粉嫩一区二区三区| 欧美精品黄色| 国产亚洲高清视频| 亚洲毛片在线观看| 午夜精品影院在线观看| 美女主播视频一区| 亚洲日本视频| 香蕉免费一区二区三区在线观看 | 亚洲淫性视频| 久久精品国产综合| 亚洲欧洲精品一区| 欧美自拍丝袜亚洲| 国产精品久久久| 亚洲国产小视频在线观看| 亚洲色图制服丝袜| 免费永久网站黄欧美| 亚洲一级网站| 欧美日韩国产区| 在线成人小视频| 性欧美xxxx视频在线观看| 亚洲福利国产精品| 欧美一区二区视频免费观看| 欧美日韩三级一区二区| 一区二区三区在线视频播放| 亚洲性图久久| 欧美电影资源| 性色av一区二区三区在线观看| 久久只有精品| 亚洲综合大片69999| 欧美女同视频| 亚洲国产日日夜夜| 久久综合导航| 久久国产欧美精品| 国产亚洲精品v| 香蕉精品999视频一区二区 | 亚洲国产裸拍裸体视频在线观看乱了中文 | 女人色偷偷aa久久天堂| 国产色视频一区| 亚洲综合色丁香婷婷六月图片| 欧美成人免费视频| 老司机精品导航| 在线观看视频亚洲| 欧美成人a视频| 欧美 亚欧 日韩视频在线| 樱桃国产成人精品视频| 麻豆91精品| 米奇777超碰欧美日韩亚洲| 激情久久久久| 欧美顶级大胆免费视频| 久久久久久久久久看片| 狠色狠色综合久久| 久久综合一区二区| 老牛嫩草一区二区三区日本| 1204国产成人精品视频| 欧美jizzhd精品欧美喷水| 久久午夜精品一区二区| 亚洲黄色免费网站| 亚洲人精品午夜| 欧美视频一区二区在线观看| 亚洲一区二区三区四区视频| 亚洲一区二区三区四区五区午夜| 国产精品午夜在线| 老司机67194精品线观看| 免费观看国产成人| 亚洲毛片在线| 亚洲图片欧美日产| 国产一区二区黄| 亚洲国产精品免费| 国产精品国产三级国产专播精品人 | 亚洲精品一区在线观看香蕉| 欧美日韩免费在线视频| 亚洲欧美激情诱惑| 欧美有码在线观看视频| 亚洲国产视频a| 亚洲视频免费| 在线精品亚洲一区二区| 一区二区三区黄色| 亚洲午夜一区二区| 在线不卡a资源高清| 亚洲精品日日夜夜| 国产日韩在线看| 亚洲第一在线| 国产区精品视频| 亚洲人成在线观看| 国产日韩欧美综合精品| 亚洲国产精品精华液网站| 欧美亚州韩日在线看免费版国语版| 久久国产精品第一页| 美女亚洲精品| 久久精品av麻豆的观看方式| 欧美精品一区二区三区蜜桃| 欧美中文在线观看| 欧美片在线播放| 美女黄毛**国产精品啪啪| 欧美日韩美女| 欧美国产日韩精品| 国产揄拍国内精品对白| 在线亚洲高清视频| 亚洲精品男同| 久久精品视频在线播放| 亚洲专区免费| 欧美成人午夜影院| 久久综合久久综合久久综合| 欧美日韩一区国产| 亚洲国产精品日韩| 亚洲国产经典视频| 欧美中文在线观看国产| 欧美一区午夜精品| 欧美日韩国产亚洲一区| 美女精品视频一区| 国语精品中文字幕| 欧美亚洲免费电影| 欧美在线观看天堂一区二区三区| 欧美日韩一区在线播放| 亚洲国产小视频在线观看| 亚洲国语精品自产拍在线观看| 久久精品一区二区三区中文字幕| 欧美三级不卡| 亚洲精品婷婷| 亚洲午夜激情| 国产精品视频999| 中文精品视频| 午夜精品久久久久| 国产女人精品视频| 亚洲欧美精品在线| 久久九九99| 在线欧美不卡| 农夫在线精品视频免费观看| 亚洲第一色在线| 日韩视频三区| 欧美色欧美亚洲另类七区| 一区二区三区高清不卡| 欧美一级片久久久久久久| 国产欧美日韩另类一区| 久久国产精品一区二区| 女人天堂亚洲aⅴ在线观看| 极品av少妇一区二区| 巨乳诱惑日韩免费av| 亚洲电影免费在线| 亚洲一二三区在线观看| 国产日韩精品视频一区| 久久深夜福利免费观看| 亚洲国产欧美在线| 一区二区三区视频观看| 国产精品日韩| 亚洲国产精品成人综合色在线婷婷| 亚洲国产精品久久久久秋霞不卡 | 久久精品99国产精品| 奶水喷射视频一区| 一区二区三区四区五区视频 | 精品成人一区| 欧美精品在线一区二区| 亚洲一区二区在线观看视频| 久久一区二区三区四区五区| 亚洲国产精品成人综合色在线婷婷| 欧美岛国激情| 亚洲欧美一区二区三区极速播放 | 久久国产天堂福利天堂| 欧美+日本+国产+在线a∨观看| 亚洲精品专区| 国产一区二区主播在线| 欧美激情精品久久久| 亚洲欧美怡红院| 亚洲高清免费视频| 欧美一区二区视频在线观看2020 | 国产欧美日韩| 欧美韩日一区二区三区| 亚洲欧美激情在线视频| 亚洲人成在线播放| 久久一二三区| 欧美在线视频一区二区| 最新国产精品拍自在线播放| 国产精品普通话对白| 免费观看一级特黄欧美大片| 亚洲欧美成aⅴ人在线观看| 亚洲经典在线| 男女精品网站| 久久成人在线| 亚洲欧美三级在线| 一区二区三区精密机械公司 | 亚洲国产精品第一区二区三区| 国产精品videosex极品| 麻豆九一精品爱看视频在线观看免费 | 国产精品久久久久久久久久免费| 男人的天堂亚洲| 久久综合给合| 久久福利精品|