c++ - GCC: template previously defined 错误

标签 c++ linux visual-studio gcc g++

我有以下代码可以与 Visual C++ 10 一起使用,但不能与 Linux 上的 GCC 一起使用:

class basic_format 
{
    ... 
    basic_format() : str_(), fmt_() {}
    ... 

    template <class ValueT>
    basic_format& operator%(const ValueT& x) 
    {
    ...
    }

    template <class Ch, class Tr>
    friend std::basic_ostream<Ch, Tr>& operator<<(
        std::basic_ostream<Ch, Tr>& sout, const basic_format<Ch, Tr>& f) 
    {
      ...
    }
    ...
}

使用:

query << basic_format<char_type>("%s %s HTTP/%.1f\r\n") % method % path % this->version();

编译器大喊:

Multiple markers at this line
    - ‘template<class Ch, class Tr> std::basic_ostream<_CharT, _Traits>& clx::operator<<(std::basic_ostream<_CharT, _Traits>&, const clx::basic_format<Ch, 
     Tr>&)’ previously defined here
    - redefinition of ‘template<class Ch, class Tr> std::basic_ostream<_CharT, _Traits>& clx::operator<<(std::basic_ostream<_CharT, _Traits>&, const 
     clx::basic_format<Ch, Tr>&)’

我正在使用 GCC 4.4.7 我可以做些什么来避免 GCC 上的这个错误吗?

最佳答案

GCC 4.9.3,编译并运行良好:

#include <iostream>

template<class Ch = char, class Tr = std::char_traits<char>>
class basic_format
{
    template <class C, class T>
    friend std::basic_ostream<C, T>& operator<<(
            std::basic_ostream<C, T>& sout, const basic_format<C, T>& f)
    {
        return sout << "lol";
    }
};

int main()
{
    std::cout << basic_format<>() << std::endl;
}

关于c++ - GCC: template previously defined 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36223096/

相关文章:

c++ - 合并 K 排序列表尝试

linux - 编写 unix 脚本自动发送电子邮件,传递参数

c++ - 在 Visual C++ 2003 中删除调试符号

c++ - LoadString、静态库和可执行文件

c++ - 使用概念化方法显式实例化类

c++ - 使用专用和非专用类型的模板重载

django - 安装 django-CMS 后 .iPython 的权限被拒绝

asp.net - 如何访问Visual Studio 2015中的ASP配置工具

c++ - 在成员初始值设定项列表之前的构造函数中引发异常?

python - 在 python 中将变量写入 txt 会引发一些错误