Can you check what is wrong with this code

This is a discussion on Can you check what is wrong with this code within the C++ Programming forums, part of the General Programming Boards category; Header file Code: #ifndef STRING_HPP #define STRING_HPP #include <iostream> using namespace std; /* mystring class */ class mystring { public: ...

  1. #1
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    Can you check what is wrong with this code

    Header file
    Code:
    
    #ifndef STRING_HPP
    #define STRING_HPP
    
    #include <iostream>
    
    using namespace std;
    
    /* mystring class */
    class mystring
    {
        public:
    
        typedef struct STRING* String;
    
         /*String struct */
        struct STRING
        {
            int size;
            char *theString;
        };
    
    
    
    
    void validateString( mystring& theString );
    
    mystring()
    {
    	String->theString = NULL;
    	String->size =0;
    }
    mystring( const char *init)
    {
    	newString( init );
    }
    
    ~mystring(mystring& theString)
    {
    	destroyString( theString );
    }
    mystring&  destroyString( mystring& theString );
    mystring& newString( char* theString );
    char* charAt(  mystring& theString, int index );
    void printString( mystring& theString);
    friend mystring& operator+(  mystring& string1,  mystring& string2 );
    
    };
    
    
    
    #endif
    Function file
    Code:
    
    #include <iostream.h>
    
    #include <assert.h>
    #include "mystring.hpp"
    using namespace std;
    
    int stringCount = 0; /*Keep track of string count */
    /*------------------------------------------------------
     newString
    
     PURPOSE: Create a new string struct
     INPUT PARAMETERS:input string
     OUTPUT PARAMETERS: string object
    ------------------------------------------------------ */
    
    mystring& mystring::newString( const char* init )
    {
      String *newString = NULL;
    
      assert( init != NULL );
    
      newString = new String[init];
      assert( newString != NULL );
    
      if ( newString != NULL )
      {
        newString->theString = new char( strlen(init) + 1 ); /*Assign array size of the new string object*/
        assert( newString -> theString != NULL );
    
        if ( newString -> theString != NULL )
        {
          strcpy( newString -> theString, init );/*Copy input string to string component of the struct */
          newString -> size = strlen( newString -> theString );/*Assign size of the String object */
    
          assert( newString -> size == strlen( init ) );
    
          stringCount++;
        }
    
        else
        {
          delete newString ;
          newString = NULL;
        }
      }
    
      return( newString );
    }
    
    /*------------------------------------------------------
     validateString
    
     PURPOSE: To check if there is any content in the object or empty object */
     INPUT PARAMETERS:string object
     OUTPUT PARAMETERS: void
    ------------------------------------------------------ */
    
     void mystring::validateString(  mystring& theString )
    {
      assert( theString != NULL );
      assert( theString -> theString != NULL );
      assert( theString -> theString[theString -> size] == '\0' );
    }
    
    /*------------------------------------------------------
     destroyString
    
     PURPOSE: Destroys the string struct
     INPUT PARAMETERS:string object
     OUTPUT PARAMETERS: string object
    ------------------------------------------------------ */
    
    mystring& mystring::destroyString( mystring& theString )
    {
      validateString( theString );
      assert( stringCount > 0 );
    
      delete theString -> theString ; /*Delete String component of the struct */
      delete theString ; /*Delete the object itself */
      theString = NULL;
    
      stringCount--;
    
      return( theString );
    }
    
    
    char* mystring::charAt(  mystring& theString, int index )
    {
      validateString( theString );
      assert( index >= 0 );
      assert( index < theString -> size );
    
      return( theString -> theString[index] );
    }
    
    /*------------------------------------------------------
     operator+
     PURPOSE: Operator overloading of the operator +
     INPUT PARAMETERS:string object
     OUTPUT PARAMETERS: new string object which has contents of string1 and 2
    ------------------------------------------------------ */
    
    mystring& mystring::operator+(   mystring& string1, mystring& string2 )
    {
      String *newString = NULL;
    
      validateString( string2 );
    
      newString = new String[];
      assert( newString != NULL );
    
      if ( newString != NULL )
      {
        newString -> size = this. size + string2 -> size;
        newString -> theString = new char [newString -> size + 1];
        assert( newString -> theString != NULL );
    
        if ( newString -> theString != NULL )
        {
          strcpy( newString -> theString, string1 -> theString );
          strcpy( &(newString -> theString[string1 -> size]), string2 -> theString );
    
          assert( newString -> size == strlen( newString -> theString ) );
          assert( newString -> theString[0] == this. theString[0] );
          assert( newString -> theString[this. size] == string2 -> theString[0] );
    
          stringCount++;
        }
    
        else
        {
          delete newString ;
          newString = NULL;
        }
      }
    
      return( newString );
    }
    
    /*------------------------------------------------------
     printString
    
     PURPOSE: Print the contents of the string object
     INPUT PARAMETERS: string onject
     OUTPUT PARAMETERS: void
    ------------------------------------------------------ */
    
    void mystring::printString(  mystring& theString )
    {
      validateString( theString );
    
      cout << theString->size<<endl;
      cout << theString -> theString << endl ;
    }

  2. #2
    Allways learning cs_student's Avatar
    Join Date
    Aug 2008
    Location
    ~/
    Posts
    39
    Can you tell us if it's getting a compile error or if there is a specific run time error?

  3. #3
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    IM getting a long list of compile erros

  4. #4
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Errors
    Code:
    mystring.hpp:49: error: destructors may not have parameters
    mystring.hpp: In constructor `mystring::mystring()':
    mystring.hpp:40: error: expected primary-expression before '->' token
    mystring.hpp:41: error: expected primary-expression before '->' token
    mystring.hpp: In constructor `mystring::mystring(const char*)':
    mystring.hpp:45: error: invalid conversion from `const char*' to `char*'
    mystring.hpp:45: error:   initializing argument 1 of `mystring& mystring::newString(char*)'
    mystring.hpp: In destructor `mystring::~mystring()':
    mystring.hpp:50: error: `theString' undeclared (first use this function)
    mystring.hpp:50: error: (Each undeclared identifier is reported only once for each function it appears in.)
    mystring.cpp: At global scope:
    mystring.cpp:27: error: prototype for `mystring& mystring::newString(const char*)' does not match any in class `mystring'
    mystring.hpp:53: error: candidate is: mystring& mystring::newString(char*)
    mystring.cpp: In member function `mystring& mystring::newString(const char*)':
    mystring.cpp:32: error: expression in new-declarator must have integral or enumeration type
    mystring.cpp:37: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:38: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:40: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:42: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:43: error: request for member `size' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:43: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:45: error: request for member `size' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:57: error: invalid initialization of reference of type 'mystring&' from expression of type 'STRING**'
    mystring.cpp: At global scope:
    mystring.cpp:64: error: `INPUT' does not name a type
    mystring.cpp: In member function `mystring& mystring::destroyString(mystring&)':
    mystring.cpp:88: error: base operand of `->' has non-pointer type `mystring'
    mystring.cpp:89: error: type `class mystring' argument given to `delete', expected pointer
    mystring.cpp: In member function `char* mystring::charAt(mystring&, int)':
    mystring.cpp:102: error: base operand of `->' has non-pointer type `mystring'
    mystring.cpp:104: error: base operand of `->' has non-pointer type `mystring'
    mystring.cpp: In member function `mystring& mystring::operator+(mystring&)':
    mystring.cpp:120: error: expected primary-expression before ']' token
    mystring.cpp:125: error: request for member `size' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:125: error: request for member `size' in `this', which is of non-class type `mystring* const'
    mystring.cpp:125: error: base operand of `->' has non-pointer type `mystring'
    mystring.cpp:126: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:126: error: request for member `size' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:127: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:129: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:131: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:131: error: `string1' undeclared (first use this function)
    mystring.cpp:132: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:132: error: base operand of `->' has non-pointer type `mystring'
    mystring.cpp:134: error: request for member `size' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:134: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:135: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:135: error: request for member `theString' in `this', which is of non-class type `mystring* const'
    mystring.cpp:136: error: request for member `theString' in `*newString', which is of non-class type `STRING*'
    mystring.cpp:136: error: request for member `size' in `this', which is of non-class type `mystring* const'
    mystring.cpp:136: error: base operand of `->' has non-pointer type `mystring'
    mystring.cpp:148: error: invalid initialization of reference of type 'mystring&' from expression of type 'STRING**'
    mystring.cpp: In member function `void mystring::printString(mystring&)':
    mystring.cpp:163: error: base operand of `->' has non-pointer type `mystring'
    mystring.cpp:164: error: base operand of `->' has non-pointer type `mystring'
    *** Error code 1
    make: Fatal error: Command failed for target `mystring.o'

  5. #5
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Well you'll have to start going through them - first one is obvious enough - you destructor should not take parameters. Secondly, your class actually has no data members - only a typedef and a struct definition. Thirdly your class doesn't contain member function prototypes which you later try to define as being in the class. These are rather serious mistakes and they're only the tip of the iceberg - I'd suggest that you go back and read all the tutorials before you continue, or try those at www.cplusplus.com
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with this code?
    By Finchie_88 in forum Networking/Device Communication
    Replies: 10
    Last Post: 05-27-2005, 09:46 AM
  2. What's wrong with my Win32 Wrapper code?
    By miica in forum Windows Programming
    Replies: 9
    Last Post: 02-22-2005, 07:55 PM
  3. What's wrong with this code?
    By Tride in forum C Programming
    Replies: 5
    Last Post: 05-26-2003, 12:40 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. what's wrong with this qsort code?
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 05:58 AM

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