help with this code

This is a discussion on help with this code within the C++ Programming forums, part of the General Programming Boards category; here s this code which cause 4 errors in DevC++ and 19 errors in VC++ its all about assign values ...

  1. #1
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Post help with this code

    here s this code which cause 4 errors in DevC++ and 19 errors in VC++ its all about assign values to character array through pointers

    Code:
    #include <iostream.h>
    #include <string.h>
    
         class String
         {
            public:
               // constructors
               String();
                String(const char *const);
               String(const String &);
              ~String();
    
              // overloaded operators
              char & operator[](int offset);
              char operator[](int offset) const;
              String operator+(const String&);
              void operator+=(const String&);
              String & operator= (const String &);
    
              // General accessors
              int GetLen()const { return itsLen; }
              const char * GetString() const { return itsString; }
              // static int ConstructorCount;
    
           private:
              String (int);         // private constructor
              char * itsString;
              unsigned short itsLen;
    
        };
    
        // default constructor creates string of 0 bytes
        String::String()
        {
           itsString = new char[1];
           itsString[] = `\0';   // first error ------------------(Err 1)
           itsLen=0;
           // cout << "\tDefault string constructor\n";
           // ConstructorCount++;
        }
    
        // private (helper) constructor, used only by
        // class methods for creating a new string of
        // required size.  Null filled.
        String::String(int len)
        {
           itsString = new char[len+1];
           for (int i = 0; i<=len; i++)
              itsString[i] = `\0';        //----------------------Err(2)
           itsLen=len;
          // cout << "\tString(int) constructor\n";
          // ConstructorCount++;
        }
    
        // Converts a character array to a String
        String::String(const char * const cString)
        {
           itsLen = strlen(cString);
           itsString = new char[itsLen+1];
           for (int i = 0; i<itsLen; i++)
              itsString[i] = cString[i];
           itsString[itsLen]='\0';
           // cout << "\tString(char*) constructor\n";
           // ConstructorCount++;
       }
    
        // copy constructor
        String::String (const String & rhs)
        {
           itsLen=rhs.GetLen();
           itsString = new char[itsLen+1];
           for (int i = 0; i<itsLen;i++)
              itsString[i] = rhs[i];    
           itsString[itsLen] = `\0'; //---------------------------Err (3)
           // cout << "\tString(String&) constructor\n";
           // ConstructorCount++;
        }
    
        // destructor, frees allocated memory
        String::~String ()
        {
           delete [] itsString;
           itsLen = 0;
           // cout << "\tString destructor\n";
        }
    
        // operator equals, frees existing memory
        // then copies string and size
        String& String::operator=(const String & rhs)
        {
           if (this == &rhs)
              return *this;
           delete [] itsString;
           itsLen=rhs.GetLen();
           itsString = new char[itsLen+1];
           for (int i = 0; i<itsLen;i++)
             itsString[i] = rhs[i];
           itsString[itsLen] = `\0';// ----------------------Err(4)
           return *this;
          // cout << "\tString operator=\n";
      }
    
       //non constant offset operator, returns
       // reference to character so it can be
       // changed!
       char & String::operator[](int offset)
       {
          if (offset > itsLen)
            return itsString[itsLen-1];
          else
             return itsString[offset];
       }
    
      // constant offset operator for use
       // on const objects (see copy constructor!)
       char String::operator[](int offset) const
       {
         if (offset > itsLen)
             return itsString[itsLen-1];
          else
             return itsString[offset];
       }
       // creates a new string by adding current
      // string to rhs
       String String::operator+(const String& rhs)
      {
          int  totalLen = itsLen + rhs.GetLen();
          String temp(totalLen);
          int i, j;
          for (i = 0; i<itsLen; i++)
             temp[i] = itsString[i];
          for (j = 0; j<rhs.GetLen(); j++, i++)
             temp[i] = rhs[j];
          temp[totalLen]='\0';
          return temp;
       }
    
       // changes current string, returns nothing
       void String::operator+=(const String& rhs)
       {
          unsigned short rhsLen = rhs.GetLen();
          unsigned short totalLen = itsLen + rhsLen;
          String  temp(totalLen);
          for (int i = 0; i<itsLen; i++)
             temp[i] = itsString[i];
          for (int j = 0; j<rhs.GetLen(); j++, i++)
             temp[i] = rhs[i-itsLen];
          temp[totalLen]='\0';
          *this = temp;
       }
    
     // int String::ConstructorCount = 0;
    thanks in advance
    Programming is a high logical enjoyable art for both programer and user !!

  2. #2
    Unregistered
    Guest
    itsString[] = `\0'; // first error ------------------(Err 1)

    put a zero inside the [];

    fix that one and who knows, maybe the others go away, too. If not post the actual error messages you are getting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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