student looking for some help.

This is a discussion on student looking for some help. within the C++ Programming forums, part of the General Programming Boards category; ok i need some help this code is for a class and im lost on what the errors mean. heres ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    california
    Posts
    8

    student looking for some help.

    ok i need some help this code is for a class and im lost on what the errors mean.
    heres the errors

  2. #2
    Registered User
    Join Date
    Sep 2009
    Location
    california
    Posts
    8

    cont

    ok i need some help this code is for a class and im lost on what the errors mean.
    heres the errors

    Code:
    In member function `String& String: : DownCase(char)': 
     expected ` ) ' before '!' token 
     invalid initialization of reference of type 'String&' from expression of type 'char' 
     In member function `String& String:: operator+=(char* ) ' : 
     cannot convert `String' to `char' in assignment 
     In function `std : : istream& operator>>(std::istream&, const String&)':   
      passing `const String' as `this' argument of `String& String : : operator=(const String& ) ' discards qualifiers 
     In member function `bool String:: operator==(char*) const':   
     no match for 'operator= =' in 'this = = String(c)' 
       note  candidates are: bool operator= = ( char*, String& ) 
       note                  bool operator= = ( String & , char * ) 
     In member function `bool String: : operator= = ( char ) const': 
     no match for 'operator= = ' in ' * (const String*)this = = String(((int)c ))'   
      note  candidates are: bool String:: operator==(String&) const  
      note                  bool String:: operator= = (char * ) const  
      note                  bool String:: operator= = (char ) const 
      note                  bool operator= = (char *, String& ) 
      note                  bool operator==(String&, char*) 
     In function `bool operator= =(char * , String & ) ' :  
     no match for ' operator==' in 's == String(c)' 
      note  candidates are: bool String : : operator==(String& ) const 
      note                  bool String:: operator= = (char * ) const 
      note                  bool String:: operator= = (char ) const 
      note                  bool operator==(char*, String& )  
      note                  bool operator==(String&, char * ) 
      In function `bool operator==(String&, char * ) ':  
      
      these errors are for all of the operators 
     
      passing `const String' as `this' argument of `bool String::IsAlpha(String)' discards qualifiers 
      passing `const String' as `this' argument of `bool String::IsAlpha(String)' discards qualifiers 
      passing `const String' as `this' argument of `char String::upCase(char)' discards qualifiers   
      passing `const String' as `this' argument of `char String::upCase(char)' discards qualifiers   
      passing `const String' as `this' argument of `char String::upCase(char)' discards qualifiers   
      passing `const String' as `this' argument of `char String::upCase(char)' discards qualifiers   
     My_String.o Error 1
    and the code is next sorry its a lot of code and the main.cpp is sopost to be perty empty.
    its just going to be helping me test.

    so heres the code

    main.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "My_String.h"
    
    using namespace std;
    
    int main()
    {
        String s1("sea1dl");
        
        system("PAUSE");
        return 0;
    }
    .
    .
    .
    .
    My_String.cpp
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "My_String.h"
    
    using namespace std;
    
    
    String::String()
    {
        STR=NULL;
        Len=0;
    }
    
    String::String(char* c)
    {
        Len=0;
        for(;c[Len]!='\0'; ++Len);
        if (Len != 0)
        {
            STR= new char[Len];
            for (unsigned I=0; I <Len; ++I)
            {
               STR[I] = c[I];
            }
        }
        else
            STR=NULL;
    }
    
    String::String(char c)
    {
        Len=1;
        STR= new char[1];
        STR[0]=c;
    }
    
    String::String(const String& s)
    {
        Len=s.Len;
        STR= new char[Len];
        for (unsigned I=0; I <Len; ++I)
        {
            STR[I] = s.STR[I];
        }
    }
    
    String::~String()
    {
        Len=0;
        delete [] STR;
    }
    
    char upCase (char c)
    {
         return (c & 223);
    }
    /*
    String& String::upCase (String s)
    {
         return (s & 223);
    }
    */
    String& String::DownCase(char s)
    {
        return ( (s)! 32);
    }
    
    bool IsAlpha(String s0)
    {
         for(unsigned I=1;I<s0[0];I++)
         {
             if((s0[I] >= 'a' && s0[I] <= 'z') || (s0[I] >= 'A' && s0[I] <= 'Z'))
                return 1;
             else
                return 0;
         }
    }
    
    String& String::operator=(const String& s)
    {
        if(this == &s)
            return (*this);
        delete [] STR;
        Len=s.Len;
        STR= new char [Len];
        for (unsigned I=0; I <Len; ++I)
        {
            STR[I] = s.STR[I];
        }
        return (*this);
    }
    
    char String::operator[](unsigned I)const
    {
        if (I >Len)
            exit (1);
        return (STR [I]);
    }
    
    char& String::operator[](unsigned I)
    {
        if(I>=Len)
            exit(1);
        return (STR[I]);
    }
    /*
    String::operator_int(String)
    {
        
    }
    String::operator_float(String)
    {
        
    }
    */
    int String::position(char c)
    {
        for (unsigned I=0; I < Len; I++)
        {
            if( STR[I]==c)
            {
                return (I);
            }
            else
            {
                cout<<"not in STR posistion of "<<I<<endl<<endl;
            }
        }
    }
    
    String& String::operator+=(String s)
    {
        char* Temp = new char [Len+s.Len];
        for (unsigned I= 0; I <Len; I++){
            Temp [I]= STR[I];
        }
        for (unsigned I =0; I<s.Len; I++){
            Temp[I+Len]= s.STR[I];
        }
        Len+=s.Len;
        delete [] STR;
        STR = Temp;
        return *this;
    }
    
    String& String::operator+=(char* c)
    {
        char* Temp = new char [Len+1];
        for (unsigned I= 0; I <Len; I++){
            Temp [I]= STR[I];
        }
        Len+=1;
        Temp[Len]=String (c);
        delete [] STR;
        STR = Temp;
        return *this;
    }
    
    String& String::operator+=(char c)
    {
        char* Temp = new char [Len+1];
        for (unsigned I= 0; I <Len; I++){
            Temp [I]= STR[I];
        }
        Temp[Len+1]=c;
        Len+=1;
        delete [] STR;
        STR = Temp;
        return *this;
    }
    
    String& String::operator+(const String& s)
    {
        return(String (*this) += s);
    }
    
    String& String::operator+(char* c)
    {
        return(String (*this) += String (c));
    }
    
    String& operator+ (char* c, const String& s)
    {
        return(String (c) += s);
    }
    
    String String::upcase(void)
    {
        for (unsigned I=0; I < Len; I++)
        {
            if (STR[I] >= 'A' && STR[I] <= 'Z')
            {
                cout<<"upper case already"<<endl<<endl;
            }
            else if (STR[I] >= 'a' && STR[I] <= 'z')
            {
                 upCase(char (STR[I]));
                 cout<<STR[I]<<endl<<endl;
            }
            else
            {
                cout<<"not alpibitical"<<endl<<endl;
            }          
        }
    }
    
    String String::downcase(void)
    {
        for (unsigned I=0; I < Len; I++)
        {
            if (STR[I] >= 'A' && STR[I] <= 'Z')
            {
                DownCase(STR[I]);
                cout<<STR[I]<<endl<<endl;
            }
            else if (STR[I] >= 'a' && STR[I] <= 'z')
            {
                 cout<<"Lower case already"<<endl<<endl;
            }
            else
            {
                cout<<"not alpibitical"<<endl<<endl;
            }     
        }
    }
    
    String String::togglecase(void)
    {
        for (unsigned I=0; I < Len; I++)
        {
            if (STR[I] >= 'A' && STR[I] <= 'Z')
            {
                DownCase(char (STR[I]));
                cout<<STR[I]<<endl<<endl;
                cout<<"is now Lower case "<<endl<<endl;
            }
            else if (STR[I] >= 'a' && STR[I] <= 'z')
            {
                upCase(char (STR[I]));
                cout<<STR[I]<<endl<<endl;
                cout<<"is now upper case "<<endl<<endl;
            }
            else
            {
                cout<<"not alpibitical"<<endl<<endl;
            }     
        }
    }
    
    ostream& operator<<(ostream& Os, const String& s)
    {
        for(unsigned I=0; I < s.Len;I++)
        {
            Os<<s[I];
        }
        return (Os);
    }
    
    istream& operator>>(istream& Is, const String& s )
    {
        String Temp;
        char cc;
        while (Is.get(cc))
        {
            if (cc == '\n')
            {
                break;
            }
        }
        Temp +=cc;
        s = Temp;
        return Is;
    }
    
    
    bool String::operator==(String& s) const
    {
        if (Len != s.Len) { return (false);}
        for (unsigned I=0; I<Len; I++){
            if(STR[I]== s.STR[I]){return true;}
        }
    }
            
    bool String::operator== (char* c) const
    {
        return (this== String(c));
    }
            
    bool String::operator==(char c)const
    {
        return (*this ==String (c));
    }
            
    bool operator==(char* c, String& s)
    {
        return (s == String(c));
    }
            
    bool operator==(String& s, char* c)
    {
        return (s == String(c));
    }
            
    bool String::operator!=(String& s)const
    {
        if (Len == s.Len) { return (false);}
        for (unsigned I=0; I<Len; I++){
            if(STR[I]== s.STR[I]){return false;}
        }
        return (true);
    }
            
    bool String::operator!=(char* c)const
    {
        return (*this ==String (c));
    }
            
    bool String::operator!=(char c)const
    {
        return (*this ==String (c));
    }
            
    bool operator!=(char* c, String& s)
    {
        return (s == String(c));
    }
            
    bool operator!=(String& s, char* c)
    {
        return (s == String(c));
    }
            
    bool String::operator>(String& s)const
    {
        if (*this < s){return (false);}
        if (*this == s){ return (false);}
        return (true);
        
    }
            
    bool String::operator>(char* c)const
    {
        return (*this > String (c));
    }
            
    bool String::operator>(char c)const
    {
        return (*this > String (c));
    }
            
    bool operator>(char* c, String& s)
    {
        return (s > String(c));
    }
            
    bool operator>(String& s, char* c)
    {
        return (s > String(c));
    }
            
    bool String::operator<(const String& s)const
    {
        unsigned len=((Len<s.Len)?Len:s.Len);
        unsigned I=0;
        for (;I<len && STR[I]==s.STR[I]; I++);
        if (len==Len || len == s.Len) {return (Len<s.Len);}
        if (!IsAlpha (STR[I]) || ! IsAlpha(s.STR[I]))
        {
            return (STR[I]< s.STR[I]);
        }
        if (upCase(STR[I])== upCase(s.STR[I]))
        {
            return (upCase(STR[I])== upCase(s.STR[I]));
        }
    }
            
    bool String::operator<(char* c)const
    {
        return (*this < String (c));
    }
            
    bool String::operator<(char c)const
    {
        return (*this < String (c));
    }
           
    bool operator<(char* c, String& s)
    {
        return (s < String(c));
    }
            
    bool operator<(String& s, char* c)
    {
        return (s < String(c));
    }
            
    bool String::operator<=(String& s)const
    {
        if (*this == s && *this < s) 
        {
            return (true);
        }
        return (false);
    }
    
    bool String::operator<=(char* c)const
    {
        return (*this <= String (c));
    }
            
    bool String::operator<=(char c)const
    {
        return (*this <= String (c));
    }
            
    bool operator<=(char* c, String& s)
    {
        return (s <= String(c));
    }
            
    bool operator<=(String& s, char* c)
    {
        return (s <= String(c));
    }
            
    bool String::operator>=(String& s)const
    {
        if (*this == s && !(*this < s)) 
        {
            return (true);
        }
            return (false);
    }
            
    bool String::operator>=(char* c)const
    {
        return (*this > String (c));
    }
            
    bool String::operator>=(char c)const
    {
        return (*this > String (c));
    }
            
    bool operator>=(char* c, String& s)
    {
        return (s > String(c));
    }
              
    bool operator>=(String& s, char* c)
    {
        return (s > String(c));
    }
    .
    .
    .
    .
    .
    My_String.h
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    #ifndef _My_string_SRF_h
    #define _My_string_SRF_h
    
    /*
        class Invariant allocated(STR)==Len || (STR == NULL && Len==0)
    */
    
    
    
    class String
    {
        
        char* STR;
        unsigned Len;
        
        
        public:
        String();
        //Pre: none
        //Post: STR == NULL && Len==0
        
        String (char*);
        //Pre: c is a valid c/c++ string
        //Post: Len == strlen(c) && strcmp )static_cast<char*>(*this),c)==0
            
        String (char);
        //Pre: can not use the number zero W/o casting to a char
        //Post: len=1, STR=c
            
        String(const String&);
        //Pre: none
        //Post: *this == *s
            
        ~String();
        //Pre: none
        //Post: alocated memery is released
        
        char upCase (char );
    
        bool IsAlpha(String);
        
        //String& upCase (String );
        
        String& DownCase(char );
            
        inline unsigned Length(String s) { return Len;}
        //Pre: String has a Len
        //Post: returns *this.Len
            
        String& operator=(const String&);
        //Pre: none
        //Post: *this = *s
             
        char operator[](unsigned)const;
        //Pre: 
        //Post: 
           
        char& operator[](unsigned) ;
        //Pre: 
        //Post: 
          
        /*  
        operator_int(String);
        //Pre: 
        //Post: 
            
        operator_float(String);
        //Pre: 
        //Post: 
         */   
        int position(char);
        //Pre: 
        //Post: 
        
        String& operator+=(String );
        //Pre: 
        //Post: 
            
        String& operator+=(char*);
        //Pre: 
        //Post: 
            
        String& operator+=(char);
        //Pre: 
        //Post: 
        
        String& operator+(const String&);
        //Pre: 
        //Post: 
        
        String& operator+(char*);
        //Pre: 
        //Post: 
        
        String& operator+(char);
        //Pre: 
        //Post: 
        
        friend String& operator+(char*, const String& );
         
        String upcase(void);
        //Pre: 
        //Post: 
            
        String downcase(void);
        //Pre: 
        //Post: 
            
        String togglecase(void);
        //Pre: 
        //Post: 
           
        friend ostream& operator<< (ostream& , const String&);
        //Pre: 
        //Post: 
        
        friend istream& operator>> (istream& , const String&);
        //Pre: 
        //Post: 
        
        bool operator==(String&)const;
        //Pre: 
        //Post: 
            
        bool operator==(char*)const;
        //Pre: 
        //Post: 
            
        bool operator==(char)const;
        //Pre: 
        //Post: 
            
        friend bool operator==(char*, String&);
        //Pre: 
        //Post: 
            
        friend bool operator==(String&, char*);
        //Pre: 
        //Post: 
            
        bool operator!=(String&)const;
        //Pre: 
        //Post: 
            
        bool operator!=(char*)const;
        //Pre: 
        //Post: 
            
        bool operator!=(char)const;
        //Pre: 
        //Post: 
            
        friend bool operator!=(char*, String&);
        //Pre: 
        //Post: 
            
        friend bool operator!=(String&, char*);
        //Pre: 
        //Post: 
            
        bool operator>(String&)const;
        //Pre: 
        //Post: 
            
        bool operator>(char*)const;
        //Pre: 
        //Post: 
            
        bool operator>(char)const;
        //Pre: 
        //Post: 
            
        friend bool operator>(char*, String&);
        //Pre: 
        //Post: 
            
        friend bool operator>(String&, char*);
        //Pre: 
        //Post: 
            
        bool operator<(String&)const;
        //Pre: 
        //Post: 
            
        bool operator<(char*)const;
        //Pre: 
        //Post: 
            
        bool operator<(char)const;
        //Pre: 
        //Post: 
            
        friend bool operator<(char*, String&);
        //Pre: 
        //Post: 
            
        friend bool operator<(String&, char*);
        //Pre: 
        //Post: 
            
        bool operator<=(String&)const;
        //Pre: 
        //Post: 
    
        bool operator<=(char*)const;
        //Pre: 
        //Post: 
            
        bool operator<=(char)const;
        //Pre: 
        //Post: 
            
        friend bool operator<=(char*, String&);
        //Pre: 
        //Post: 
            
        friend bool operator<=(String&, char*);
        //Pre: 
        //Post: 
            
        bool operator>=(String&)const;
        //Pre: 
        //Post: 
            
        bool operator>=( char*)const;
        //Pre: 
        //Post: 
            
        bool operator>=(char)const;
        //Pre: 
        //Post: 
            
        friend bool operator>=(char*, String&);
        //Pre: 
        //Post: 
            
        friend bool operator>=(String&, char*);
        //Pre: 
        //Post: 
        
        
        
        
    };
    
    #endif
    thank you for your help guys
    Last edited by akairyuu; 09-27-2009 at 11:27 PM. Reason: fixed things

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    1) Welcome to CBoard
    2) You shouldn't make a new post, you should edit your original post.
    3) You should wrap code errors in a code tag (there's a button: #).
    4) Could you also post the code causing the error (the String usage and possibly the String class if it's custom)?

    It looks like common conversion issues between char, char array and a string class.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The first error should be obvious:
    Code:
     return ( (s)! 32);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    The entire definition doesn't really make any sense (to me).

    Code:
    String& String::DownCase(char s)
    {
        return ( (s)! 32);
    }
    So you're going to take an existing string object, calling a method with a character, and return an entirely new string object? I'm thinking maybe it's suppose to be a static method, or should be standalone (possibly in a "detail" namespace). Furthermore that's some type of syntax error, it's suppose to add 32 to s, I believe. Even then it's not entirely correct because you need clauses; search google for a solution imo: Let me google that for you

    Once you've figured that out, if you're stuck post your next problem imo.
    Last edited by Dae; 09-28-2009 at 03:41 AM. Reason: add not subtract
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    california
    Posts
    8
    yeah that is one of them but im not sure how to write it i found that online and have been trying to get it to work but ive failed. so i don't know how to write that in the first place and this is the first time that i have had to do something like this with out tolower() able to be used. i know im suppose to do something with 32 and i think im suppose to add it to the the upper case letter but because of my string class i can get it to work right.
    and i know that that is because im going item by item and changing each one buy its self but thats what i need it to do.

    got it!!!!! yay ok so now the next one.

    Code:
    String& String::operator+=(char* c)
    {
        char* Temp = new char [Len+1];
        for (unsigned I= 0; I <Len; I++){
            Temp [I]= STR[I];
        }
        Len+=1;
        Temp[Len]=String (c);  //this is where the error is.
        delete [] STR;
        STR = Temp;
        return *this;
    }
    
    /*
      In member function `String& String::operator+=(char*)': 
         cannot convert `String' to `char' in assignment 
    */
    i under stand what its saying i just have know idea where to start to fix this

    again thanks for the help
    Last edited by akairyuu; 09-28-2009 at 11:13 AM. Reason: fixed the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 05:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 09:43 PM
  5. A few tips please...
    By gems in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 03:28 PM

Tags for this Thread


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