![]() |
| | #1 |
| Registered User Join Date: Sep 2009 Location: california
Posts: 7
| student looking for some help. heres the errors |
| akairyuu is offline | |
| | #2 |
| Registered User Join Date: Sep 2009 Location: california
Posts: 7
| 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 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
Last edited by akairyuu; 09-27-2009 at 10:27 PM. Reason: fixed things |
| akairyuu is offline | |
| | #3 |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| 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.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101 |
| Dae is offline | |
| | #4 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| 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 |
| CornedBee is offline | |
| | #5 |
| Deprecated Join Date: Oct 2004 Location: Canada
Posts: 944
| The entire definition doesn't really make any sense (to me). Code: String& String::DownCase(char s)
{
return ( (s)! 32);
}
Once you've figured that out, if you're stuck post your next problem imo.
__________________ Warning: Have doubt in anything I post. GCC 4.5.0 (lambda branch), Boost 1.40.0, Code::Blocks 8.02, Ubuntu 9.04 010001000110000101100101 Last edited by Dae; 09-28-2009 at 02:41 AM. Reason: add not subtract |
| Dae is offline | |
| | #6 |
| Registered User Join Date: Sep 2009 Location: california
Posts: 7
| 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
*/
again thanks for the help Last edited by akairyuu; 09-28-2009 at 10:13 AM. Reason: fixed the code |
| akairyuu is offline | |
![]() |
| Tags |
| c++, object, string |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Updating in a sequential file? | Ronnyv1 | C Programming | 1 | 03-24-2009 04:41 PM |
| Database assignment is Killing me! | Boltrig | C Programming | 2 | 11-29-2007 03:56 AM |
| LinkList Sorting in C | simly01 | C Programming | 3 | 11-25-2002 01:21 PM |
| Creating a student grade book-how? | Hopelessly confused | C Programming | 5 | 10-03-2002 08:43 PM |
| A few tips please... | gems | C++ Programming | 4 | 04-11-2002 02:28 PM |