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