• <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++博客 :: 首頁(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è)程序員是鏈接不過(guò),
            錯(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   博問(wèn)   Chat2DB   管理


            香港aa三级久久三级| 岛国搬运www久久| 三上悠亚久久精品| 亚洲天堂久久精品| 77777亚洲午夜久久多人| 久久99精品久久久久久久久久 | 久久久噜噜噜久久| 狠狠色狠狠色综合久久| 久久免费精品一区二区| 久久无码专区国产精品发布 | 久久久久久久免费视频| 久久ww精品w免费人成| 亚洲精品视频久久久| 亚洲精品高清久久| 久久久久亚洲AV片无码下载蜜桃| 久久毛片免费看一区二区三区| 日韩精品久久久肉伦网站| 无码乱码观看精品久久| 国产91色综合久久免费| 久久精品国产99国产精品导航| 国内精品久久久久国产盗摄| 97精品久久天干天天天按摩| 伊人色综合久久天天人手人婷 | 久久天天躁狠狠躁夜夜躁2O2O| 亚洲人成无码网站久久99热国产| 亚洲伊人久久大香线蕉苏妲己| 亚洲AV日韩AV天堂久久| 国产69精品久久久久久人妻精品 | 亚洲精品美女久久777777| 日本加勒比久久精品| 国产综合免费精品久久久| 秋霞久久国产精品电影院| 国产精品一久久香蕉国产线看| 亚洲伊人久久精品影院| 久久久久久国产精品美女| 久久天天躁狠狠躁夜夜2020一| 亚洲国产精品一区二区三区久久| 久久久久久a亚洲欧洲aⅴ| 久久精品国产精品青草app| 国产91久久精品一区二区| 久久成人影院精品777|