【发布时间】:2016-07-13 09:29:57
【问题描述】:
我有以下代码可用于 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 上出现这个错误吗?
【问题讨论】:
-
详细说明你想做什么。运算符的声明在哪里,是否在其他类中?这段代码的目的是什么 - 与一些现有的运算符成为朋友或定义新的朋友运算符或替换现有的运算符?
-
@user2807083 这两个定义在同一个类中。我使用这两个模板来格式化 HTTP 请求,如问题所示。
标签: c++ linux visual-studio gcc g++