473,803 Members | 3,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

overloading >> operator

hi guys,

i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istr eam &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 2084
Hello

"Ashwin" <as************ @gmail.comwrote in message
news:11******** *************@b 28g2000cwb.goog legroups.com...
hi guys,

i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istr eam &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******** *************@b 28g2000cwb.goog legroups.com...
hi guys,

i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istr eam &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>>(istr eam &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******** *************@b 28g2000cwb.goog legroups.com...
hi guys,

i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istr eam &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******** *************@b 28g2000cwb.goog legroups.com...
hi guys,

i have overloaded >operator for my own string class .the function is
as given below
istream& operator>>(istr eam &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=(strin g1 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=(strin g1 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=(strin g1 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

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

Similar topics

8
1772
by: maths_fan | last post by:
Can't understand how to overload these operations and for what reason?
4
1881
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& lhs, class1& rhs) { .....random code here return lhs:} //code for class2 private: class1 jimbo;
4
2627
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 syntax; >, <, | That really shouldn't be too hard to wrap in a class, but so far I didn't succeed to do so this well, since I'm facing some trouble with operator precedence that I do not know how to overcome.
3
385
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 for example the statement IntArray *x; Please give me some help regarding this ////////////////////////////////////////////////////////////////////////////////////////////////////////// class IntArray
2
3549
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 difference ... right?
3
2070
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
2506
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 meant for formatted text input and output? Let's imagine, for example, that I had a standard Windows BMP file (I am aware that C++ has no concept of a BMP - this is simply putting my question in a simple context). If I wanted to load it into a...
11
4147
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 mind) and it would be fairly generic. The only sidecase I can see is that operator. itself would not be looked up through operator. . I read that there was previous debate on the subject, but I haven't been able to find why it was rejected.
0
2033
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 C#. #ifndef _OFFMAT_HPP_ #define _OFFMAT_HPP_ #include "fdct_wrapping_inc.hpp" using std::ostream; using std::istream;
0
10316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.