gpt4 book ai didi

c++ - GCC: template previously defined 错误

转载 作者:太空狗 更新时间:2023-10-29 12:42:46 26 4
gpt4 key购买 nike

我有以下代码可以与 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com