i write this program and i cant solve the errors in program.
in subprogram 'string2.cpp' geting errors of lines 'cin>>s3' and 's2="My name is"+s3'.
what porblems are in 'operator+' and 'operator>>' functions?
It's not very easy to help you as you didn't post any code - the zip file doesn't have it either.
Post the relevant code here, so we can compile it ourselves, in [ code ][ /code ] tags (but without the spaces). And indicate to us which lines have the compile errors.
i write this program and i cant solve the errors in program.
in subprogram 'string2.cpp' geting errors of lines 'cin>>s3' and 's2="My name is"+s3'.
what porblems are in 'operator+' and 'operator>>' functions?
Please be more specific. Compiler errors? Runtime errors? You need to mention exactly what errors you're seeing.
But I took a look at the code, and there are a few issues:
1) There is nothing Visual C++ specific about your code. It is all generic C++ -- next time, post in the non-Visual C++ forum.
2) This is wrong:
Code:
s2 ="My name is "+s3; // overloaded =, + operators
The overloaded + is not called, at least the one that you wrote doesn't get called.
The + operator in the line above takes a const char* on the left side, and a String on the right of the + sign. This means you have to write an overloaded + operator that takes two parameters:
Code:
String operator+(const char*, const String &);
This must be a standalone function (a friend of the String class).
This is a memory leak. You're allocating with new[], but then you're losing the pointer by reassigning str. Just because the allocation with new is 0, C++ still must assign a unique pointer from operator new[], regardless of whether it is 0.
So this one line program is no good:
Code:
int main()
{
String s;
}
You wind up calling delete[] on a NULL pointer, but the pointer that you did allocate was never deleted.
What happens if new[] fails on the second line that's in red? An exception is thrown, so what happened to str? You deleted it, and you can never recover it. This means your String object is in an inconsistent state -- it has a pointer to str that is no good, and the string it used to point to is gone.
You do the same mistake here:
Code:
String& String::operator =(const String& st)
{
if(this==&st)
return *this;
len=st.len;
delete [] str; // your string is gone.
str=new char[len+1]; // what if this fails??
strcpy(str,st.str);
return *this;
}
You're supposed to allocate everything you need first to a temp pointer, then when finished, you delete [] str and then assign str to the temp pointer. Your code only works as long as new[] doesn't fail, and you don't write classes that use dynamically allocated memory this way.
5) This is no good if I pass NULL:
Code:
String::String(const char *s)
{
len=strlen(s);
If I pass a NULL pointer to this constructor, you then go ahead and try to get the strlen() of a NULL pointer. If the code doesn't crash, then the behaviour is undefined as to what happens. You're supposed to check first if the pointer is NULL before calling strlen().
6) Where is operator += in your code? Any good string class has an operator +=. Instead, you made operator + act as if it is operator +=
The operator + is supposed to return a brand new String, not change the existing string. This goes back to item 2) above. Your String class needs to implement + and +=, and implement versions that take two parameters.
These are just a few of the runtime issues I see with your class.
Also, you do know there is a std::string class that does all of this already, right? So why are you trying to code your own String class, when honestly, writing proper string classes should be left to the advanced (or close to advanced) C++ programmer? If this is homework, there are thousands of examples of String classes, not 100% coded correctly, but still show how to properly overload +, +=, etc. Why not look at those examples?
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 18th, 2011 at 02:50 PM.
i write this program and i cant solve the errors in program.
in subprogram 'string2.cpp' geting errors of lines 'cin>>s3' and 's2="My name is"+s3'.
what porblems are in 'operator+' and 'operator>>' functions?
I don't know what error you mean for the line 'cin >> s3', as this line compiles for me. (Whether it runs properly is another matter entirely, but if it doesn't you need to specify what is wrong with it).
As for the second line, 's2="My name is "+s3', this doesn't work because you haven't defined a method to cover adding a string literal ("My name is ") and a String (s3). The method
Code:
String& operator+(const String &);
can only be used to add two Strings.
To fix this, you can either use the String constructor 'String(const char *s)', like so:
Please be more specific. Compiler errors? Runtime errors? You need to mention exactly what errors you're seeing.
But I took a look at the code, and there are a few issues:
1) There is nothing Visual C++ specific about your code. It is all generic C++ -- next time, post in the non-Visual C++ forum.
2) This is wrong:
Code:
s2 ="My name is "+s3; // overloaded =, + operators
The overloaded + is not called, at least the one that you wrote doesn't get called.
The + operator in the line above takes a const char* on the left side, and a String on the right of the + sign. This means you have to write an overloaded + operator that takes two parameters:
Code:
String operator+(const char*, const String &);
This must be a standalone function (a friend of the String class).
This is a memory leak. You're allocating with new[], but then you're losing the pointer by reassigning str. Just because the allocation with new is 0, C++ still must assign a unique pointer from operator new[], regardless of whether it is 0.
So this one line program is no good:
Code:
int main()
{
String s;
}
You wind up calling delete[] on a NULL pointer, but the pointer that you did allocate was never deleted.
What happens if new[] fails on the second line that's in red? An exception is thrown, so what happened to str? You deleted it, and you can never recover it. This means your String object is in an inconsistent state -- it has a pointer to str that is no good, and the string it used to point to is gone.
You do the same mistake here:
Code:
String& String::operator =(const String& st)
{
if(this==&st)
return *this;
len=st.len;
delete [] str; // your string is gone.
str=new char[len+1]; // what if this fails??
strcpy(str,st.str);
return *this;
}
You're supposed to allocate everything you need first to a temp pointer, then when finished, you delete [] str and then assign str to the temp pointer. Your code only works as long as new[] doesn't fail, and you don't write classes that use dynamically allocated memory this way.
5) This is no good if I pass NULL:
Code:
String::String(const char *s)
{
len=strlen(s);
If I pass a NULL pointer to this constructor, you then go ahead and try to get the strlen() of a NULL pointer. If the code doesn't crash, then the behaviour is undefined as to what happens. You're supposed to check first if the pointer is NULL before calling strlen().
6) Where is operator += in your code? Any good string class has an operator +=. Instead, you made operator + act as if it is operator +=
The operator + is supposed to return a brand new String, not change the existing string. This goes back to item 2) above. Your String class needs to implement + and +=, and implement versions that take two parameters.
These are just a few of the runtime issues I see with your class.
Also, you do know there is a std::string class that does all of this already, right? So why are you trying to code your own String class, when honestly, writing proper string classes should be left to the advanced (or close to advanced) C++ programmer? If this is homework, there are thousands of examples of String classes, not 100% coded correctly, but still show how to properly overload +, +=, etc. Why not look at those examples?
Regards,
Paul McKenzie
thanks a lot
these are comiler error.
i try to correct code and i put correct code here. if you see bug in this code, you tell me.
code have comiler errors, for example: "Error3error C2248: 'String::len' : cannot access private member declared in class 'String'c:\documents and settings\fahimeh\my documents\visual studio 2008\projects\test(pr2_chap12)\test\string2.cpp65test"\
why cannot access to "total.len" or "total.str"?
the program runs but after take s3 variable, the program stops and the next line not applicable, why?
the operator=() function what do you think instead of code(delete [] str)?
operator=() function is in the C++ primer plus ebook(chapter 12) and this is practice of ebook.
Last edited by fshb; December 19th, 2011 at 01:19 AM.
You are returning a reference to an object that goes out of scope. You cannot return references to local objects. You were already told that operator + returns a brand new string, not a reference.
Here is what I think -- get another book, a book that preferably teaches C++ in a way where you're writing programs, not fighting with String classes. A book such as "Accelerated C++" is one such book.
As far as I know, that book/ebook you're using is obsolete, and you're learning C++ using old, outdated material. It makes no sense to be using material that is:
1) almost 20 years old.
2) Instead of learning, we're correcting obvious mistakes in the book. You can't learn from a book that has these mistakes.
That book teaches C++ in a style that basically went out of style (or should have went out of style) years ago. That book may have had its day back in the early 1990's, but it really shouldn't be used for learning C++ in this day and age. C++ is a language that has evolved from the days when it looked almost exactly like 'C', except for a few keywords. Reading up on up-to-date material (at least material that existed after C++ became standardized in 1998) should be the only consideration when learning the language.
You're supposed to be learning how to write programs that use the C++ library. and only when you have reached intermediate status do you even consider writing classes that have the complexity of String classes. The reason being what you're seeing here on this thread -- you may end up writing a class that is wrong, but "works", and then walk away believing you've written good code when you haven't written such code.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; December 19th, 2011 at 12:54 PM.
You are returning a reference to an object that goes out of scope. You cannot return references to local objects. You were already told that operator + returns a brand new string, not a reference.
You have two operator = functions. Which one?Here is what I think -- get another book, a book that preferably teaches C++ in a way where you're writing programs, not fighting with String classes. A book such as "Accelerated C++" is one such book.
As far as I know, that book is obsolete, and you're learning C++ using old, outdated material. It makes no sense to be using material that is
1) almost 20 years old.
2) Instead of learning, we're correcting obvious mistakes in the book. You can't learn from a book that has these mistakes.
That book teaches C++ in a style that basically went out of style (or should have went out of style) years ago.
You're supposed to be learning how to write programs that use the C++ library. and only when you have reached intermediate status do you even consider writing classes that have the complexity of String classes. The reason being what you're seeing here on this thread -- you may end up writing a class that is wrong, but "works", and then walk away believing you've written good code when you haven't written such code.
Regards,
Paul McKenzie
thanks
what is last version ebook? can you put link download?
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.