Compiler not creating templated ostream << operator

心已入冬 提交于 2019-12-23 21:04:03

问题


I have a class, defined in a head as:

template <typename T> class MyClass
{
   template <typename U> friend std::ostream& operator<<(std::ostream& output, const MyClass<U>& p);
   public:
      ...
}

In an implementation file, I have:

template <typename U> std::ostream& operator<<(std::ostream& output, const MyClass<U>& m)
{
   output << "Some stuff";
   return output;
}

Which all looks fairly kosher. However, when I try and use this operator (i.e. std::cout << MyClass()), I get the following linker error:

Undefined symbols: std::basic_ostream<char, std::char_traits<char> >& operator<< <InnerType>(std::basic_ostream<char, std::char_traits<char> >&, MyClass<InnerType> const&)

I am suprised the compiler hasn't automagicially generated this for me... Any suggestions as to what I'm doing wrong?


回答1:


In an implementation file, I have:

That's the problem. You can't split template definitions between header and implementation files. Due to the nature of templates, C++ compilers are finicky here. Define all the code in the header to make it work.

In fact, the problem here is that all template definitions must reside within the same compilation unit because the C++ standard doesn't define how template information are shared across different units. These units are stitched together by the linker, but generics are resolved at compile time (which is earlier), not at link time.

Theoretically, the C++ standard defines a keyword, export, to handle these cases. In practice, no compiler implements this (with one exception?), and there is no intention to change this because the cost/usefulness trade-off is not considered good enough.




回答2:


Too many templates - this works:

#include <iostream>
using namespace std;
template <typename T> struct MyClass {

    friend ostream & operator << ( ostream & os, MyClass<T> & c ) {
        os << "MyClass\n";
        return os;
    }
};

int main() {
    MyClass <int> c;
    cout << c;
}


来源:https://stackoverflow.com/questions/551156/compiler-not-creating-templated-ostream-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!