String Class

This is a discussion on String Class within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Daved Do you mean String(char*)? If so, I'm not sure you need it. The String(const char*) should ...

  1. #16
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    Do you mean String(char*)? If so, I'm not sure you need it. The String(const char*) should work for both char* and const char*. If you really do mean String(char), then I'm not sure if you need that either. Do you really want to initialize your string with a single character? If you do, then treat it like a string with one character but instead of using strcpy just assign that character to the first index in the array you allocate and add the null as the second.
    Then would this be right?

    Code:
    String::String(char nme)
    {
    	buf = new char[];
    	buf[0] = nme;
    	buf[1] = NULL;
    }

  2. #17
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Now maybe I just suck at C++, and that's probably true, but it seems completely backwards and stupid to allocate an array of an unknown size and then mess with the first two bytes.

    Hint: Allocate the 2 bytes you're using.

  3. #18
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    '\0' is the null character, which is what I was referring to when I said null. I think NULL would probably end up working in C++, but I would stick with '\0' instead. That is of course in addition to MacGyver's hint.

  4. #19
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by MacGyver View Post
    Now maybe I just suck at C++, and that's probably true, but it seems completely backwards and stupid to allocate an array of an unknown size and then mess with the first two bytes.

    Hint: Allocate the 2 bytes you're using. ;)
    That is really weird. I could've sworn I put: buf = new char[1];

    Well, now I am getting these errors: How do I fix them?

    warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data

    for this code:

    Code:
    stringlength = strlen(str);

  5. #20
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    /me bashes keyboard into monitor while yelling, "Two! Two, I said! Two!"

    Make stringlength a size_t variable instead of an int.

  6. #21
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by MacGyver View Post
    /me bashes keyboard into monitor while yelling, "Two! Two, I said! Two!"

    Make stringlength a size_t variable instead of an int.
    if I make stringlength a size_t variable then it screws the rest of my code up. I have to have stringlength be an int.

  7. #22
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    How so? What does it mess up?

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    if I make stringlength a size_t variable then it screws the rest of my code up. I have to have stringlength be an int.
    Change the rest of your code. If you do not want to be necessarily stuck with std::size_t, you could use a typedef, say size_type, such that the member function getLength() returns a String::size_type instead of an int, and this would also be the type of stringlength.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Okay, Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    Frankly, I would remove the operator++ and operator--, they do not typically make sense where strings are concerned.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    I have to have it. The ++ operators are supposed to add one to each character in the string literal pointed at by buf. The --operators subtract one from each character.
    Does anybody seen any errors or have any other suggestions?

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?
    Yes, there are errors. Your operator++ functions add to the size of the string, but you don't increase the size of your array. You need to reallocate like you do in oeprator+=.

    Also, I would imagine that you would want operator+ functions that take a const char* as one of the parameters so you can add a string literal or C style string to your class.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    >> Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?
    Yes, there are errors. Your operator++ functions add to the size of the string, but you don't increase the size of your array. You need to reallocate like you do in oeprator+=.

    Also, I would imagine that you would want operator+ functions that take a const char* as one of the parameters so you can add a string literal or C style string to your class.
    Although this is not even remotely what the code you posted earlier actually DOES to the string...

    Have you thought about writing some code that verifies the functionality, e.g.

    Code:
    ... 
    // Test concatenation with +=
      String s1, s2;
    
      s1 = "abc"; s2 = "def";
      s1 += s2;
      if (s1 != "abcdef") report_error(String("operator+="), s1, String("abcdef"));
    ...
    The report_error function would do something like this:
    Code:
    void report_error(String func, String res, String expected)  {
       cout << "*** Error ***" << "Failed function " << function << " expected " << expected << " got " << res << endl;
      errcnt++;
    }
    I'd suggest that you do several testcases for each operator and basic function (such as constructors), and make sure that each does what you want.

    It is part of good programming practice to write tests for the module or functions that you produce, and it's a good habit to do proper testing even when it's a "hobby project". You will get much better reputation amongst your collegues/customers/friends if you test your code properly before committing it to be used by others.

    --
    Mats

  14. #29
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    >> Can anybody else make suggestions on my +=, ++, or + operators? Are there any errors?
    Yes, there are errors. Your operator++ functions add to the size of the string, but you don't increase the size of your array. You need to reallocate like you do in oeprator+=.

    Also, I would imagine that you would want operator+ functions that take a const char* as one of the parameters so you can add a string literal or C style string to your class.
    Can you look at the code below to see if this is correct?

    Code:
    String& String::operator ++()
    {
    	buf[stringlength] = 'X';
    	stringlength++;
    	temp = new char[stringlength];
    	strcpy(temp, buf);
    	return *this;
    }

  15. #30
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    That's the idea, but it's not quite correct yet. You are replacing the terminating null character with 'X', strcpy needs the terminating null to know how many characters to copy, so by replacing the null character you will confuse strcpy and you will read and write past the bounds of your arrays.

Page 2 of 8 FirstFirst 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21