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. 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
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.
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.
"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
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
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.
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.
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.
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'. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: maths_fan |
last post by:
Can't understand how to overload these operations and for what reason?
|
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;
|
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.
|
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
|
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?
| |
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 );
|
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...
|
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.
|
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;
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |