Global function definition in header file - how to avoid duplicated symbol linkage error

后端 未结 4 1387
执念已碎
执念已碎 2020-11-29 11:41

I have the following code in a header only file.

#pragma once

class error_code {
public:
    unsigned __int64 hi;
    unsigned __int64 lo;    
};

std::ostr         


        
4条回答
  •  既然无缘
    2020-11-29 11:49

    Either make the function inline:

    inline std::ostream& operator<< (std::ostream& o, const error_code& e) {
        return o << "[" << e.hi << "," << e.lo << "]";
    }
    

    or make it a template function:

    template
    std::basic_ostream& operator<< (std::basic_ostream& o,
                                           const error_code& e) {
        return o << "[" << e.hi << "," << e.lo << "]";
    }
    

提交回复
热议问题