943,699 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2050
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jul 8th, 2009
0

Re: String Method Question

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jul 8th, 2009
0

Re: String Method Question

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*â
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 8th, 2009
0

Re: String Method Question

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jul 8th, 2009
0

Re: String Method Question

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!
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 9th, 2009
0

Re: String Method Question

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?

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 9th, 2009
0

Re: String Method Question

>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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 9th, 2009
1

Re: String Method Question

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Jul 9th, 2009
0

Re: String Method Question

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Jul 9th, 2009
0

Re: String Method Question

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009
Jul 9th, 2009
0

Re: String Method Question

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, 5 views)
File Type: h MyString.h (1.0 KB, 5 views)
File Type: cpp assign3.cpp (2.7 KB, 3 views)
Reputation Points: 10
Solved Threads: 0
Junior Poster
lancevo3 is offline Offline
146 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: using ## in statements
Next Thread in C++ Forum Timeline: Class inheritance finding





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC