String Method Question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String Method Question

 
0
  #11
Jul 8th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: String Method Question

 
0
  #12
Jul 8th, 2009
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*â
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String Method Question

 
0
  #13
Jul 8th, 2009
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: String Method Question

 
0
  #14
Jul 8th, 2009
I went ahead and got rid of the comparison because I was looking through notes and saw that in this case it is not needed. Still having issues with the other errors though. I'm going to keep playing with it though, I'll be on here if anyone has anymore advice. Thanks!
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: String Method Question

 
0
  #15
Jul 9th, 2009
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?

  1. MyString::MyString(const char* s)
  2. {
  3. size_t stringSize = strlen(s);
  4. *stringStorage = new char[stringSize+1];
  5. for(int i = 0; stringSize > stringStorage; i++)
  6. char[stringSize+1] = s;
  7. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: String Method Question

 
0
  #16
Jul 9th, 2009
>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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,755
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: String Method Question

 
1
  #17
Jul 9th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: String Method Question

 
0
  #18
Jul 9th, 2009
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.
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."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: String Method Question

 
0
  #19
Jul 9th, 2009
Ya I don't know why he wants us to ignore the null terminator but he does. So I am at a standstill with this for now I guess. any idea how I could code this for loop? I cant really go any farther without it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 136
Reputation: lancevo3 is an unknown quantity at this point 
Solved Threads: 0
lancevo3 lancevo3 is offline Offline
Junior Poster

Re: String Method Question

 
0
  #20
Jul 9th, 2009
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
Attached Files
File Type: cpp MyString.cpp (2.7 KB, 1 views)
File Type: h MyString.h (1.0 KB, 1 views)
File Type: cpp assign3.cpp (2.7 KB, 1 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1449 | Replies: 52
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC