MyString Woes

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

MyString Woes

 
0
  #1
Jan 16th, 2007
I was absent for the discussion and lab due to illness and I am at a total loss as to how to do this assignment.

Implement the my_string class in header file my_string.h, so that the test code given below reports SUCCESS.

  1. // test_my_string.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <cassert>
  6. #include "my_string.h"
  7.  
  8. using namespace std;
  9.  
  10. //#define STRING string
  11. #define STRING my_string
  12.  
  13. int main()
  14. {
  15. STRING s1; // s1 == ""
  16. assert(s1.length() == 0);
  17.  
  18. STRING s2("hi"); // s2 == "hi"
  19. assert(s2.length() == 2);
  20.  
  21. STRING s3(s2); // s3 == "hi"
  22. assert(s3.length() == 2);
  23. assert(s3[0] == 'h');
  24. assert(s3[1] == 'i');
  25.  
  26. s1 = s2; // s1 == "hi"
  27.  
  28. s3 = "bye"; // s3 == "bye"
  29. assert(s3.length() == 3);
  30. assert(s3[0] == 'b');
  31. assert(s3[1] == 'y');
  32. assert(s3[2] == 'e');
  33.  
  34. s1 += "re"; // s1 == "hire"
  35. assert(s1.length() == 4);
  36. assert(s1[0] == 'h');
  37. assert(s1[1] == 'i');
  38. assert(s1[2] == 'r');
  39. assert(s1[3] == 'e');
  40.  
  41. cout << "SUCCESS" << endl;

I cannot get the program to get past

assert(s3[0] == 'h');

nor can I get cout << s3[0]; to work.

What am I missing here?
Last edited by FC Jamison; Jan 16th, 2007 at 1:54 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: MyString Woes

 
0
  #2
Jan 16th, 2007
Nevermind...

I got it to work using

  1. char &operator[](unsigned int index) {
  2. return str[index];
  3. }
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum


Views: 1039 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC