473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

overloading >> operator

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.

Jul 27 '06 #1
11 2037
Hello

"Ashwin" <as************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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;
}
Very bad code.
1. memory leaks
2. can buffer overflow
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.
Are you entering a string bigger than 100 characters?

Can't you trace your code and see where exactly the crash occurs?

--
Elias
Jul 27 '06 #2
lallous wrote:
Hello

"Ashwin" <as************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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;
}

Very bad code.
1. memory leaks
2. can buffer overflow
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.

Are you entering a string bigger than 100 characters?

Can't you trace your code and see where exactly the crash occurs?

--
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.

Jul 27 '06 #3
Ashwin wrote:
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;
Don't do this, use getline();

--
Ian Collins.
Jul 27 '06 #4
"Ashwin" <as************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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;
Ashwin,
Try doing deep copy instead of shallow copy. And don't forget to delete the
allocated memory.
return in;
}
HTH,
Sukumar R
Jul 27 '06 #5
WittyGuy wrote:
"Ashwin" <as************@gmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
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;

Ashwin,
Try doing deep copy instead of shallow copy. And don't forget to delete the
allocated memory.
return in;
thanks for the reponse can anyone tell me how to implement the
following eqation
s[1]='a';
where s is a object of the string class i have written
plzz tell me how to overload the operations just signatures of the
functions.
}

HTH,
Sukumar R
Jul 27 '06 #6
Ashwin wrote:
>
thanks for the reponse can anyone tell me how to implement the
following eqation
s[1]='a';
where s is a object of the string class i have written
plzz tell me how to overload the operations just signatures of the
functions.
Show us what you have so far.

Please stop using silly abbreviations like 'plz' and try to use correct
capitalisation. Otherwise your posts are hard to read an people who
might help you will ignore them.
--
Ian Collins.
Jul 27 '06 #7

Ian Collins wrote:
Ashwin wrote:

thanks for the reponse can anyone tell me how to implement the
following eqation
s[1]='a';
where s is a object of the string class i have written
plzz tell me how to overload the operations just signatures of the
functions.
Show us what you have so far.

Please stop using silly abbreviations like 'plz' and try to use correct
capitalisation. Otherwise your posts are hard to read an people who
might help you will ignore them.
--
Ian Collins.
sorry Ian collins i wont use that abbrevation again . i have overloaded
both [ ] and = operator as follows
void operator=(char *s);
void operator=(string1 s);
and
string1& operator+(char *s);
char operator[](unsigned int i);
where string1 is the string class i am writing.
so to implement the expression s[1]='c';
i donot know how to overload the operators
i am thinking of using char operator=(char ch);
and char operator[ ] (unsigned int i);
i don't know whether it will work or not have to try.

Jul 27 '06 #8
Ashwin wrote:
>
Ian Collins wrote:
>Ashwin wrote:
>
thanks for the reponse can anyone tell me how to implement the
following eqation
s[1]='a';
where s is a object of the string class i have written
plzz tell me how to overload the operations just signatures of the
functions.
Show us what you have so far.

Please stop using silly abbreviations like 'plz' and try to use correct
capitalisation. Otherwise your posts are hard to read an people who
might help you will ignore them.
--
Ian Collins.
sorry Ian collins i wont use that abbrevation again . i have overloaded
both [ ] and = operator as follows
void operator=(char *s);
Should better be:

string1& operator=(const char* s);
void operator=(string1 s);
string1& operator=(const string1& s);
and
string1& operator+(char *s);
string1& operator+(const char *s);
char operator[](unsigned int i);
char operator[](unsigned int i) const;
where string1 is the string class i am writing.
so to implement the expression s[1]='c';
i donot know how to overload the operators
Try to add:

char& operator[](unsigned int i);
i am thinking of using char operator=(char ch);
and char operator[ ] (unsigned int i);
i don't know whether it will work or not have to try.
Doesn't make much sense.

Jul 27 '06 #9

Rolf Magnus wrote:
Ashwin wrote:

Ian Collins wrote:
Ashwin wrote:

thanks for the reponse can anyone tell me how to implement the
following eqation
s[1]='a';
where s is a object of the string class i have written
plzz tell me how to overload the operations just signatures of the
functions.

Show us what you have so far.

Please stop using silly abbreviations like 'plz' and try to use correct
capitalisation. Otherwise your posts are hard to read an people who
might help you will ignore them.
--
Ian Collins.
sorry Ian collins i wont use that abbrevation again . i have overloaded
both [ ] and = operator as follows
void operator=(char *s);

Should better be:

string1& operator=(const char* s);
void operator=(string1 s);

string1& operator=(const string1& s);
and
string1& operator+(char *s);

string1& operator+(const char *s);
char operator[](unsigned int i);

char operator[](unsigned int i) const;
where string1 is the string class i am writing.
so to implement the expression s[1]='c';
i donot know how to overload the operators

Try to add:

char& operator[](unsigned int i);
i am thinking of using char operator=(char ch);
and char operator[ ] (unsigned int i);
i don't know whether it will work or not have to try.

Doesn't make much sense.
hi Rolf,

i wanted to know how to overload the [] and = operator so that i can
implement the expression
s[1]='c'.

Jul 27 '06 #10
Ashwin wrote:
>>>i am thinking of using char operator=(char ch);
and char operator[ ] (unsigned int i);
i don't know whether it will work or not have to try.

Doesn't make much sense.

hi Rolf,

i wanted to know how to overload the [] and = operator so that i can
implement the expression
s[1]='c'.
This isn't trivial, you have to differentiate between lvalue and rvalue
usage of operator[]. The normal technique is to return a proxy object
which supports assignment from char and conversion to char.

The best explanation I have seen of this in in Scott Meyers book "More
Effective C++"

--
Ian Collins.
Jul 27 '06 #11
Ashwin wrote:
sorry Ian collins i wont use that abbrevation again . i have overloaded
both [ ] and = operator as follows
void operator=(char *s);

Should better be:

string1& operator=(const char* s);
void operator=(string1 s);

string1& operator=(const string1& s);
and
string1& operator+(char *s);

string1& operator+(const char *s);
char operator[](unsigned int i);

char operator[](unsigned int i) const;
where string1 is the string class i am writing.
so to implement the expression s[1]='c';
i donot know how to overload the operators

Try to add:

char& operator[](unsigned int i);
i am thinking of using char operator=(char ch);
and char operator[ ] (unsigned int i);
i don't know whether it will work or not have to try.

Doesn't make much sense.
hi Rolf,

i wanted to know how to overload the [] and = operator so that i can
implement the expression
s[1]='c'.
And I told you that you can't do it that way, but need to define a

char& operator[](unsigned int i);

instead.

Jul 27 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
4
by: Don Hedgpeth | last post by:
Here's a question - I'm new to c++ and I have two classes that overload the >> operator. One class calls the other...such as. //code for class1 friend std::istream& operator >> (std::istream&...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
2
by: nayannovellus | last post by:
As we know that both copy constructors and overloading = opeartor copies one object to another then whats the difference between copy constructor and overloading = operator. There must be some...
3
by: Aff | last post by:
hi, i wold like to ask, why is it when overloading operator we send int value is there any way around?. e.g. class abc { protected: int *a; private: int operator(int );
2
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
0
by: citystud | last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code. I don't really know how to implement the operator = and operator () in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.