| | |
String Method Question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
Without listing the first few errors you receive it's not possible to say with surety what you have going. However, the object you have used called this is a pointer and rightOp is apparently a C styled string. As such you can't derefernce a C styled string. What you probable want is to see if the address stored in this and the address of rightOp are the same address (ie, are the same object).
Last edited by Lerner; Jul 8th, 2009 at 6:06 pm.
Klatu Barada Nikto
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
Sorry about that the errors I am getting are listed below. Thanks.
MyString.cpp: In member function âconst MyString& MyString::operator=(const char*)â:
MyString.cpp:59: error: ISO C++ forbids comparison between pointer and integer
MyString.cpp:62: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:63: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:64: error: request for member âstringStorageâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:65: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp: In member function âconst MyString& MyString::operator=(const char*)â:
MyString.cpp:59: error: ISO C++ forbids comparison between pointer and integer
MyString.cpp:62: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:63: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:64: error: request for member âstringStorageâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:65: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
Yup, that's what the first error message says to me. You could try something like this:
if(this != rightOp)
sinc the name of an array acts as an address for the first element of that array. Hopefully they will allow comparisons of two addresses. If that doesn't work, then someone else can probably give you the correct version.
Alternatively, since this will never be a simple C Style string it can never have the same address as a C Style string and therefore you could probably get by without the comparison. If you are assigning one MyString to another, however, then it could in theory be the same address, so looking for identity is recommended.
if(this != rightOp)
sinc the name of an array acts as an address for the first element of that array. Hopefully they will allow comparisons of two addresses. If that doesn't work, then someone else can probably give you the correct version.
Alternatively, since this will never be a simple C Style string it can never have the same address as a C Style string and therefore you could probably get by without the comparison. If you are assigning one MyString to another, however, then it could in theory be the same address, so looking for identity is recommended.
Klatu Barada Nikto
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
MyString::MyString(const char* s)
This constructor takes a pointer to a constant C-style string as its argument. It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.
I was told strcpy does not work for this method and that I for loop should be used. I get everything coded correctly except the for loop which has me a bit confused. Any suggestions?
This constructor takes a pointer to a constant C-style string as its argument. It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.
I was told strcpy does not work for this method and that I for loop should be used. I get everything coded correctly except the for loop which has me a bit confused. Any suggestions?
C++ Syntax (Toggle Plain Text)
MyString::MyString(const char* s) { size_t stringSize = strlen(s); *stringStorage = new char[stringSize+1]; for(int i = 0; stringSize > stringStorage; i++) char[stringSize+1] = s; }
>It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.
Why would someone want to do that?
You have to allocate space for the null terminator as well.
Otherwise you'll run into problems while displaying the string using cout for example.
Why would someone want to do that?
You have to allocate space for the null terminator as well.
Otherwise you'll run into problems while displaying the string using cout for example.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Jul 2005
Posts: 1,755
Reputation:
Solved Threads: 283
I am certainly no expert in STL, but it is my understanding that the const char * embedded in the STL string class isn't necessarily implemented as a C style string----that is, it isn't necessarily a null terminated char array. To output such an object you could overload the << operator using a loop in the method body to display each char separately, just like you could use a loop rather than strcpy() to assign/copy the object. I suspect if you look under the hood of strcpy() you will find a loop as well, but we're even further out of my surety level on that speculaton. The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements. Independent confirmation not a bad idea before spreading this around too far is a good idea.
Klatu Barada Nikto
Well, as long as you hold the correct number of characters in a variable, this would be no problem, but a null-terminator takes 1 byte extra space, and you won't have to add several other routines to make the string display correctly on the screen.
But, of course, if his assignment is to do it without a null-terminator, then my advise probably doesn't make much sense
>The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements.
Yeah, probably his teacher wants that he avoids functions from the standard function library.
But, of course, if his assignment is to do it without a null-terminator, then my advise probably doesn't make much sense

>The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements.
Yeah, probably his teacher wants that he avoids functions from the standard function library.
Last edited by tux4life; Jul 9th, 2009 at 4:18 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
Join Date: Feb 2009
Posts: 136
Reputation:
Solved Threads: 0
These are the three files I have for this program so far I am also getting a segmentation fault error, so if anyone is willing to look at these that be great. I attached them to this post with the link to the assignment if needed. Also, assign3.cpp is given by my teacher so I did not have to do with the coding on that.
http://www.cs.niu.edu/~mcmahon/cs241...as3241m09.html
http://www.cs.niu.edu/~mcmahon/cs241...as3241m09.html
![]() |
Similar Threads
- Quick Method Question (Java)
- Need some help with a trivial dll/string method. (C#)
- Method question (Java)
- a question (Java)
- extracting an arbitrary number of numbers from a string (C)
- String.replace() Method (Java)
- Simply question: How do I return a string from a function (C)
- String literial question (PHP)
Other Threads in the C++ Forum
- Previous Thread: using ## in statements
- Next Thread: Class inheritance finding
Views: 1449 | Replies: 52
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






