• <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>
            posts - 9, comments - 0, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            類模板中的友元函數

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

            99久久做夜夜爱天天做精品| 99久久精品九九亚洲精品| 无码8090精品久久一区| 青青热久久国产久精品 | 91麻豆精品国产91久久久久久 | 久久精品国内一区二区三区| 99久久精品这里只有精品| 日韩欧美亚洲国产精品字幕久久久| 免费无码国产欧美久久18| 国产精品青草久久久久婷婷| 99久久综合国产精品免费| 99久久99久久精品国产| 亚洲性久久久影院| 久久久中文字幕| 亚洲欧美伊人久久综合一区二区| 一本伊大人香蕉久久网手机| 亚洲日本va中文字幕久久| 久久国产成人| 日韩一区二区久久久久久| 久久综合给合久久狠狠狠97色69 | 久久亚洲中文字幕精品有坂深雪| 国产女人aaa级久久久级| 久久男人Av资源网站无码软件| 亚洲国产精品无码久久久久久曰| 国产精品久久久久影视不卡| 国产偷久久久精品专区| 一97日本道伊人久久综合影院| 久久国产乱子精品免费女| 99久久精品国内| 97久久超碰国产精品旧版| 久久久精品2019免费观看| 久久综合香蕉国产蜜臀AV| 中文字幕久久波多野结衣av| 亚洲欧美另类日本久久国产真实乱对白 | 国产精品欧美久久久天天影视 | 综合人妻久久一区二区精品| 亚洲精品午夜国产va久久| 亚洲欧美久久久久9999| 狠狠综合久久综合88亚洲| 久久久久久国产精品无码下载| 久久无码AV一区二区三区|