ostream,istream和String ^ [英] ostream, istream, and String^

查看:113
本文介绍了ostream,istream和String ^的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些都不起作用(我正在使用带有clr:/纯语法的VS C ++ .NET 2005 Express):


ostream& operator<<(ostream& output,String ^ str)

{

output<< str; //编译错误

返回输出;

}

istream&运算符>>(istream& input,String ^ str)

{

input>> str; //编译错误

返回输入;

}


什么是正确的语法?


[== P ==]

解决方案

我完全将其切换为新语法,所以这就是现在的问题:


ostream%operator<<(ostream%output,String ^ str)

{

输出<< str; //编译错误

返回输出;

}

istream%operator>>(istream%input,String ^ str)

{

输入>> str; //编译错误

返回输入;

}

感谢您的回复!


[= = P ==]


" Peteroid" < PE ************ @ msn.com>在消息中写道

新闻:Oq ************* @ TK2MSFTNGP10.phx.gbl ...

这些都不起作用(我正在使用VS C ++。NET 2005 Express与clr:/ pure
语法):

ostream& operator<<(ostream& output,String ^ str)
{
output<< str; //编译错误
返回输出;
}

istream&运算符>>(istream& input,String ^ str)
{
输入>> str; //编译错误
返回输入;




[== P ==]



" Peteroid" < PE ************ @ msn.com>写了

我把它完全改为新语法,所以现在问题是:

ostream%operator<<(ostream%output,String ^ str)
{
输出<< str; //编译错误
返回输出;
}

istream%operator>>(istream%input,String ^ str)

输入>> str; //编译错误
返回输入;
}



您是否愿意解释您看到的错误?我没有看到任何错误

使用上述语法。我不知道你想要做什么,但

的实现看起来毫无意义。除非ADL在另一个命名空间中找到与
相同签名的重载(在这种情况下,运算符将是b / b
将是ambigious),实现会递归调用自己。


你可能想要的东西是

ostream%operator<<(ostream%output,String ^ str)

{

pin_ptr< const unsigned char> s =

& System :: Text :: Encoding :: ASCII-> GetBytes(str)[0];


返回

输出<< static_cast< const char *>(static_cast< const void *>(s));

}


第二个签名没有意义。字符串是不可变的。你好b / b
什么都得不到。签名应该看起来像


istream%operator>>(istream%input,String ^%str)

{

std :: string s;

输入>> s;

str = gcnew String(s.c_str());

}


-hg


>您是否愿意解释您看到的错误?


我指出的那些,都是''没有超载''<<''
字符串^>存在
或''>>''。

我不知道你在做什么重新尝试实现,但
实现看起来毫无意义。


让我用适当的代码向你展示它。此代码确实有效:

ostream%operator<<(ostream%output,int value)
{
输出<<价值; // ok
返回输出;
}


这会将输出流作为参数给出,使用ITS''<<'''operator
表示''int''并将''value''添加到流中,然后返回结果

流。这是进行流式传输的标准方法,例如将

值串行保存到文件时。

实现递归调用自己。



方法实现中使用的''<<''''''<<'''这个

方法定义。

所以我的问题是因为String ^没有''<<''

的自然重载,用于将其添加到流中。这就是我需要的,是一种通过流(两个方向)发送/接收字符串^ b
的方法。我猜测问题的一部分

问题是String ^的长度和可变性都是可变的...


[== P ==]

" Holger Grund" <豪********** @ remove.ix-n.net>在消息中写道

news:OB ************** @ TK2MSFTNGP15.phx.gbl ..." Peteroid" < PE ************ @ msn.com>写了

我把它完全改为新语法,所以现在问题是:

ostream%operator<<(ostream%output,String ^ str)
{
输出<< str; //编译错误
返回输出;
}

istream%operator>>(istream%input,String ^ str)

输入>> str; //编译错误
返回输入;
}


您是否愿意解释您看到的错误?我没有看到任何错误
上面的语法。我不知道你想要取得什么成果,但
实施看起来毫无意义。除非ADL在另一个命名空间中找到了相同签名的重载(在这种情况下,运算符将是ambigious),实现会递归地调用自己。

你可能会想要的东西就像ostream%operator<<(ostream%output,String ^ str)
{
pin_ptr< const unsigned char> s =
& System :: Text :: Encoding :: ASCII-> GetBytes(str)[0];

返回
输出<< static_cast< const char *>(static_cast< const void *>(s));


第二个签名没有意义。字符串是不可变的。你没有得到任何东西。签名应该看起来像

istream%operator>>(istream%input,String ^%str)
{
std :: string s;
输入>> s;
str = gcnew字符串(s.c_str());
}

-hg



These don''t work (I''m using VS C++.NET 2005 Express with clr:/pure syntax):

ostream& operator <<( ostream& output, String^ str )
{
output << str ; //compile error
return output ;
}
istream& operator >>( istream& input, String^ str )
{
input >> str ; // compile error
return input ;
}

What is the correct syntax?

[==P==]

解决方案

I switched this entirely to the new syntax, so this is the question now:

ostream% operator <<( ostream% output, String^ str )
{
output << str ; //compile error
return output ;
}
istream% operator >>( istream% input, String^ str )
{
input >> str ; // compile error
return input ;
}
Thanks for responses!

[==P==]

"Peteroid" <pe************@msn.com> wrote in message
news:Oq*************@TK2MSFTNGP10.phx.gbl...

These don''t work (I''m using VS C++.NET 2005 Express with clr:/pure
syntax):

ostream& operator <<( ostream& output, String^ str )
{
output << str ; //compile error
return output ;
}
istream& operator >>( istream& input, String^ str )
{
input >> str ; // compile error
return input ;
}

What is the correct syntax?

[==P==]



"Peteroid" <pe************@msn.com> wrote

I switched this entirely to the new syntax, so this is the question now:

ostream% operator <<( ostream% output, String^ str )
{
output << str ; //compile error
return output ;
}
istream% operator >>( istream% input, String^ str )
{
input >> str ; // compile error
return input ;
}


Would you care to explain which error you see? I don''t see anything wrong
with the above syntax. I don''t know what you''re trying to achive, but the
implementation looks quite pointless. Unless ADL finds an overload with
the same signature in another namespace (in which case the operators
will be ambigious), the implementations recursively call themselves.

You''ll probably want something along the lines of
ostream% operator<<( ostream% output, String^ str)
{
pin_ptr<const unsigned char> s =
&System::Text::Encoding::ASCII->GetBytes( str )[0];

return
output << static_cast<const char*>(static_cast<const void*>(s));
}

The second signature does not make sense. Strings are immutable. You
won''t get anything. The signature should look like

istream% operator >>( istream% input, String^% str )
{
std::string s;
input >> s;
str = gcnew String( s.c_str() );
}

-hg


> Would you care to explain which error you see?

The ones I indicated, both of which are something like ''no overload of ''<<''
or ''>>'' exists for String^".

I don''t know what you''re trying to achive, but the
implementation looks quite pointless.
Let me expalin it to you with code that does work. This code does work:
ostream% operator <<( ostream% output, int value)
{
output << value ; //ok
return output ;
}
This takes the output stream given as a parameter, uses ITS ''<<'' operator
for ''int'' and adds ''value'' to the stream, and then returns the resulting
stream. This is the standard way of doing streaming, such as when saving
values serially to a file.
the implementations recursively call themselves.


The ''<<'' used in the implementation of the method is not the same ''<<'' this
method defines.

So my problem is because String^ does not have a natural overload of ''<<''
which is used to add it to streams. THAT is what I need, is a way to
send/recieve String^''s through a stream (both directions). I''m guessing part
of the problem is that String^ are both variable in length and mutable...

[==P==]
"Holger Grund" <ho**********@remove.ix-n.net> wrote in message
news:OB**************@TK2MSFTNGP15.phx.gbl... "Peteroid" <pe************@msn.com> wrote

I switched this entirely to the new syntax, so this is the question now:

ostream% operator <<( ostream% output, String^ str )
{
output << str ; //compile error
return output ;
}
istream% operator >>( istream% input, String^ str )
{
input >> str ; // compile error
return input ;
}


Would you care to explain which error you see? I don''t see anything wrong
with the above syntax. I don''t know what you''re trying to achive, but the
implementation looks quite pointless. Unless ADL finds an overload with
the same signature in another namespace (in which case the operators
will be ambigious), the implementations recursively call themselves.

You''ll probably want something along the lines of
ostream% operator<<( ostream% output, String^ str)
{
pin_ptr<const unsigned char> s =
&System::Text::Encoding::ASCII->GetBytes( str )[0];

return
output << static_cast<const char*>(static_cast<const void*>(s));
}

The second signature does not make sense. Strings are immutable. You
won''t get anything. The signature should look like

istream% operator >>( istream% input, String^% str )
{
std::string s;
input >> s;
str = gcnew String( s.c_str() );
}

-hg



这篇关于ostream,istream和String ^的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆