重载>>操作者 [英] overloading >> operator
问题描述
大家好,
i为我自己的字符串类重载了>运算符。函数是
如下所示
istream&运算符>>(istream& in,string1& s)
{
char * a;
a = new char [100] ;
in>> a;
s = a;
返回;
}
i也尝试使用[100]然后使用>> a;
但在这两种情况下如果我输入一个字符串就会给出分段错误。
,如果我输入一个字符,它可以正常工作。如果我给出多个
字符,它会给出分段错误。这个问题的原因是什么?
解释。
提前感谢。
hi guys,
i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istream &in,string1 &s)
{
char *a;
a=new char[100];
in>>a;
s=a;
return in;
}
i have also tried using a[100] and then using in>>a;
but in both the cases if i enter a string it gives segmentation fault.
and if i enter a character it works properly. if i give more than one
character it gives segmentation fault.what is the reason for this plzz
explain.
thanks in advance.
推荐答案
你好
" Ashwin" < as ************ @ gmail.comwrote in message
news:11 ****************** ***@b28g2000cwb.googlegro ups.com ...
Hello
"Ashwin" <as************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
嗨伙计们,
i已超载>运营商我自己的字符串类。函数是
如下所示
istream&运算符>>(istream& in,string1& s)
{
char * a;
a = new char [100] ;
in>> a;
s = a;
返回;
}
hi guys,
i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istream &in,string1 &s)
{
char *a;
a=new char[100];
in>>a;
s=a;
return in;
}
非常糟糕的代码。
1.内存泄漏
2.缓冲区溢出
Very bad code.
1. memory leaks
2. can buffer overflow
i也试过使用[100]然后使用>> a;
但是在这两种情况下如果我输入一个字符串就会给出分段错误。
如果我输入一个字符就可以正常工作。如果我给出不止一个
字符,它会给出分段错误。这个问题的原因是什么?
解释。
i have also tried using a[100] and then using in>>a;
but in both the cases if i enter a string it gives segmentation fault.
and if i enter a character it works properly. if i give more than one
character it gives segmentation fault.what is the reason for this plzz
explain.
您输入的字符串是否超过100个字符?
您无法查看代码并查看崩溃的确切位置吗?
-
Elias
Are you entering a string bigger than 100 characters?
Can''t you trace your code and see where exactly the crash occurs?
--
Elias
lallous写道:
lallous wrote:
Hello
" Ashwin" < as ************ @ gmail.comwrote in message
news:11 ****************** ***@b28g2000cwb.googlegro ups.com ...
Hello
"Ashwin" <as************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
嗨伙计们,
i已超载>运营商我自己的字符串类。函数是
如下所示
istream&运算符>>(istream& in,string1& s)
{
char * a;
a = new char [100] ;
in>> a;
s = a;
返回;
}
hi guys,
i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istream &in,string1 &s)
{
char *a;
a=new char[100];
in>>a;
s=a;
return in;
}
非常糟糕的代码。
1.内存泄漏
2.可缓冲溢出
Very bad code.
1. memory leaks
2. can buffer overflow
i也试过使用[100]然后使用>> a;
但是在这两种情况下如果我输入一个字符串就会给出分段错误。
如果我输入一个字符就可以正常工作。如果我给出一个以上的
字符,它会给出分段错误。这个问题的原因是什么?
解释。
i have also tried using a[100] and then using in>>a;
but in both the cases if i enter a string it gives segmentation fault.
and if i enter a character it works properly. if i give more than one
character it gives segmentation fault.what is the reason for this plzz
explain.
您输入的字符串是否超过100个字符?
您无法查找代码并查看确切的位置崩溃发生了吗?
-
Elias
Are you entering a string bigger than 100 characters?
Can''t you trace your code and see where exactly the crash occurs?
--
Elias
感谢elias
你可以解释一下内存泄漏或给我任何关于
内存泄漏的好链接。我认为有一个缓冲区溢出我正在检查它。
thanks elias
can u explain me about memory leaks or give me any good links about
memory leak.i think there is a buffer overflow i am checking for it.
Ashwin写道:
Ashwin wrote:
嗨伙计们,
i已经超载>运算符为我自己的字符串类。函数是
如下所示
istream&运算符>>(istream& in,string1& s)
{
char * a;
a = new char [100] ;
in>> a;
hi guys,
i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istream &in,string1 &s)
{
char *a;
a=new char[100];
in>>a;
不要这样做,使用getline();
-
伊恩柯林斯。
Don''t do this, use getline();
--
Ian Collins.
这篇关于重载>>操作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!