字符串流问题

发布于 2024-09-07 05:33:39 字数 892 浏览 1 评论 0原文

我无法编译以下 stringstreamm

stringstream qss;

qss.operator <<  "some text " ::stringstream.operator << DDateTime::date2Oracle(dFrom) ::stringstream.operator <<  " more text " ::stringstream.operator <<  DDateTime::date2Oracle(dUntil);

如果我只使用 << 运算符而不使用 ::stringstream.operator 它会抱怨该运算符不明确,现在它会抱怨关于不正确的语法...

error C2143: syntax error : missing ';' before 'std::stringstream'

编辑:

错误 C2593:“运算符 <<”含糊不清 c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\ostream(434): 可能是 'std::basic_ostream<_Elem,_Traits>::_Myt &std::basic_ostream<_Elem,_Traits>::运算符 <<(std::basic_ostream<_Elem,_Traits>::_Mysb *)' 和 [ _Elem=字符, _Traits=std::char_traits ]

I cannot get the following stringstreamm to compile

stringstream qss;

qss.operator <<  "some text " ::stringstream.operator << DDateTime::date2Oracle(dFrom) ::stringstream.operator <<  " more text " ::stringstream.operator <<  DDateTime::date2Oracle(dUntil);

If I just use the << operator without the ::stringstream.operatorit complains about the operator being ambigious, now it complains about incorrect syntax...

error C2143: syntax error : missing ';' before 'std::stringstream'

EDIT:

error C2593: 'operator <<' is ambiguous
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\ostream(434): could be 'std::basic_ostream<_Elem,_Traits>::_Myt &std::basic_ostream<_Elem,_Traits>::operator <<(std::basic_ostream<_Elem,_Traits>::_Mysb *)'
with
[
_Elem=char,
_Traits=std::char_traits
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

酒解孤独 2024-09-14 05:33:39

operator 关键字不属于此处,请将它们排除在外:

qss << "some text" << DDateTime::date2Oracle(dFrom) << " more text " <<  DDateTime::date2Oracle(dUntil);

这应该是完全有效且明确的,除非 date2Oracle 函数被模糊重载。

为类型 T 实现 operator<< 的正确模式是:

template<typename Char, typename Traits>
std::basic_ostream<Char, Traits>
operator<<(std::basic_ostream<Char, Traits>& stream, const T& object) {
  // now put something into the stream
  return stream;   // return stream << xyz ... is also possible
}

The operator keywords don't belong here, leave them out:

qss << "some text" << DDateTime::date2Oracle(dFrom) << " more text " <<  DDateTime::date2Oracle(dUntil);

This should be perfectly valid and unambiguous, unless the date2Oracle function is ambiguously overloaded.

The correct pattern for implementing operator<< for a type T is:

template<typename Char, typename Traits>
std::basic_ostream<Char, Traits>
operator<<(std::basic_ostream<Char, Traits>& stream, const T& object) {
  // now put something into the stream
  return stream;   // return stream << xyz ... is also possible
}
幽梦紫曦~ 2024-09-14 05:33:39

嗯,很明显,无论 DDateTime::date2Oracle(dFrom) 返回什么类型都不会实现 << 运算符。所以你必须自己写一份。

至于语法,首先你必须像调用函数一样调用它,它实际上是:

stringstream qss;
operator<<(
     (operator<<(qss <<  "some text ", 
                 DDateTime::date2Oracle(dFrom)) << " more text "),
      DDateTime::date2Oracle(dUntil));

其次,stringstream定义在std命名空间中,所以你有像 std::stringstream::std::stringstream 一样编写。 ::stringstream 将在全局命名空间中查找它,并且那里没有定义这样的类。

顺便说一句,operator<< 通常作为自由函数实现,因此 qss.operator<< 不起作用。

Well, it is obvious that whatever type DDateTime::date2Oracle(dFrom) returns does not implement << operator. So you will have to write one yourself.

As for the syntax, first of all you have to call it just like a function which it actually is:

stringstream qss;
operator<<(
     (operator<<(qss <<  "some text ", 
                 DDateTime::date2Oracle(dFrom)) << " more text "),
      DDateTime::date2Oracle(dUntil));

And second of all, stringstream defined in std namespace, so you have to write it like std::stringstream or ::std::stringstream. ::stringstream will look for it in global namespace and there is no such class defined there.

BTW, operator<< usually is implemented as free function, so qss.operator<< wouldn't work.

你与昨日 2024-09-14 05:33:39

变得非常时髦:

qss.operator <<("some text ");
qss.operator <<(DDateTime::date2Oracle(dFrom));
qss.operator <<(" more text "); 
qss.operator <<(DDateTime::date2Oracle(dUntil));

你可能会更好地了解歧义所在。

Go really funky:

qss.operator <<("some text ");
qss.operator <<(DDateTime::date2Oracle(dFrom));
qss.operator <<(" more text "); 
qss.operator <<(DDateTime::date2Oracle(dUntil));

And you'll probably get a better idea where the ambiguity is.

日裸衫吸 2024-09-14 05:33:39

您需要像调用函数一样调用运算符。

std::stringstream s;

operator<<(s, "Your string")

You need to call the operator like a function.

std::stringstream s;

operator<<(s, "Your string")
妄断弥空 2024-09-14 05:33:39

显式转换为/构造字符串:

qss << "some text " << string(DDateTime::date2Oracle(dFrom)) 
    <<  " more text " <<  string(DDateTime::date2Oracle(dUntil));

Cast to / construct string explicitly:

qss << "some text " << string(DDateTime::date2Oracle(dFrom)) 
    <<  " more text " <<  string(DDateTime::date2Oracle(dUntil));
遗忘曾经 2024-09-14 05:33:39

qss << 应该还不够“一些文字”<< DDateTime...

Shouldn't be enough with qss << "some text " << DDateTime...?

顾冷 2024-09-14 05:33:39

是什么阻止你做:

stringstream s;
s << "some text" << (DDateTime::date2Oracle(dFrom)) << "more text" << (DDateTime::date2Oracle(dUntil)) ;

What's stopping you from doing:

stringstream s;
s << "some text" << (DDateTime::date2Oracle(dFrom)) << "more text" << (DDateTime::date2Oracle(dUntil)) ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文