ok i need some help this code is for a class and im lost on what the errors mean.
heres the errors
Printable View
ok i need some help this code is for a class and im lost on what the errors mean.
heres the errors
ok i need some help this code is for a class and im lost on what the errors mean.
heres the errors
and the code is next sorry its a lot of code and the main.cpp is sopost to be perty empty.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
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
thank you for your help guysCode:
#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
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.
The first error should be obvious:
Code:return ( (s)! 32);
The entire definition doesn't really make any sense (to me).
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 youCode:String& String::DownCase(char s)
{
return ( (s)! 32);
}
Once you've figured that out, if you're stuck post your next problem imo.
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.
i under stand what its saying i just have know idea where to start to fix thisCode: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
*/
again thanks for the help