hi, i am working on my coursework, my lecturer has advised me to use his data type "String" which has the corresponding String00.h and String00.cpp. i try to compile my program but get this message:
: error C2011: 'String' : 'class' type redefinition
surely my lecturer knows what he is doing. if this string.h already exists in the c++ library, why would he define it again?
i tried to rename the whole data type as String2 but still i get the same compile error.
what can you advise? i have to use this class in my project.
below is the implementation of the string00.h and string00.cpp :
// String00.h
#include <iostream.h>
#include <string.h>
class String
{
public:
String() ; // default constructor
String( const char* ) ; // conversion constructor from a C-type string
String( const char ) ; // conversion constructor from a single character
String( const String& ); // copy constructor
~String() ; // destructor
int length() const { return len ; } // returns length of a string
String& operator=( const String& ) ; // assignment operator
friend int operator==( const String&, const String& ) ; // equality operator
friend int operator!=( const String&, const String& ) ; // inequality operator
friend int operator<( const String&, const String& ) ; // comparison operator
friend int operator>( const String&, const String& ) ; // comparison operator
friend int operator<=( const String&, const String& ) ; // comparison operator
friend int operator>=( const String&, const String& ) ; // comparison operator
char& operator[]( int i ) ; // access to component character - wise?
char* operator()() const { return rep ;} // convert to a C-style string - do not use!
String getString() { return (String) rep ; } // this is the operation you want!
void setString( char* s ) { strcpy( rep, s ) ; } // this might be of use too!
friend String operator+( const String l, const String r ) ; // concatenation
friend ostream& operator<<( ostream&, const String& ) ; // output operator
friend istream& operator>>( istream&, String& ) ; // input operator
private:
int len ; // store the length of the string
char* rep ; // store the characters as a C-style string
} ;
// ----------------------------------------------------
#include "String00.h"
String::String()
{
len = 0 ;
rep = new char[1] ;
rep[0] = '\0' ;
}
String::String(const char* cp)
{
len = strlen(cp) ;
rep = new char[len + 1] ;
strcpy(rep, cp) ;
}
String::String(const char c)
{
len = 1 ;
rep = new char[2] ;
rep[0] = c ;
rep[1] = '\0' ;
}
String::String(const String& s)
{
len = s.len ;
rep =new char[len + 1] ;
strcpy(rep, s.rep) ;
}
String& String:perator=(const String& s)
{
if (this == &s)
return *this ;
delete[] rep ;
len = s.len ;
rep =new char[len + 1] ;
strcpy(rep, s.rep) ;
return *this ;
}
String::~String() { delete [] rep ; }
int operator==( const String& l, const String& r ) // equality operator
{
return ( strcmp( l.rep, r.rep ) == 0 ) ;
}
int operator!=( const String& l, const String& r ) // inequality operator
{
return ( strcmp( l.rep, r.rep ) != 0 ) ;
}
int operator<( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) < 0 ) ;
}
int operator>( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) > 0 ) ;
}
int operator<=( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) <= 0 ) ;
}
int operator>=( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) >= 0 ) ;
}
char& String:perator[]( int i ) { return rep ; } // access to component character
ostream& operator<<(ostream& os, const String& s)
{
return os << s.rep ;
}
istream& operator>>(istream& is, String& s)
{
char buffer[256] ;
is >> buffer ;
s = buffer ;
return is ;
}
String operator+( const String l, const String r )
{
char* buff = new char[l.length() + r.length() + 1] ;
strcpy( buff, l() ) ;
strcat( buff, r() ) ;
return String( buff ) ;
}
: error C2011: 'String' : 'class' type redefinition
surely my lecturer knows what he is doing. if this string.h already exists in the c++ library, why would he define it again?
i tried to rename the whole data type as String2 but still i get the same compile error.
what can you advise? i have to use this class in my project.
below is the implementation of the string00.h and string00.cpp :
// String00.h
#include <iostream.h>
#include <string.h>
class String
{
public:
String() ; // default constructor
String( const char* ) ; // conversion constructor from a C-type string
String( const char ) ; // conversion constructor from a single character
String( const String& ); // copy constructor
~String() ; // destructor
int length() const { return len ; } // returns length of a string
String& operator=( const String& ) ; // assignment operator
friend int operator==( const String&, const String& ) ; // equality operator
friend int operator!=( const String&, const String& ) ; // inequality operator
friend int operator<( const String&, const String& ) ; // comparison operator
friend int operator>( const String&, const String& ) ; // comparison operator
friend int operator<=( const String&, const String& ) ; // comparison operator
friend int operator>=( const String&, const String& ) ; // comparison operator
char& operator[]( int i ) ; // access to component character - wise?
char* operator()() const { return rep ;} // convert to a C-style string - do not use!
String getString() { return (String) rep ; } // this is the operation you want!
void setString( char* s ) { strcpy( rep, s ) ; } // this might be of use too!
friend String operator+( const String l, const String r ) ; // concatenation
friend ostream& operator<<( ostream&, const String& ) ; // output operator
friend istream& operator>>( istream&, String& ) ; // input operator
private:
int len ; // store the length of the string
char* rep ; // store the characters as a C-style string
} ;
// ----------------------------------------------------
#include "String00.h"
String::String()
{
len = 0 ;
rep = new char[1] ;
rep[0] = '\0' ;
}
String::String(const char* cp)
{
len = strlen(cp) ;
rep = new char[len + 1] ;
strcpy(rep, cp) ;
}
String::String(const char c)
{
len = 1 ;
rep = new char[2] ;
rep[0] = c ;
rep[1] = '\0' ;
}
String::String(const String& s)
{
len = s.len ;
rep =new char[len + 1] ;
strcpy(rep, s.rep) ;
}
String& String:perator=(const String& s)
{
if (this == &s)
return *this ;
delete[] rep ;
len = s.len ;
rep =new char[len + 1] ;
strcpy(rep, s.rep) ;
return *this ;
}
String::~String() { delete [] rep ; }
int operator==( const String& l, const String& r ) // equality operator
{
return ( strcmp( l.rep, r.rep ) == 0 ) ;
}
int operator!=( const String& l, const String& r ) // inequality operator
{
return ( strcmp( l.rep, r.rep ) != 0 ) ;
}
int operator<( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) < 0 ) ;
}
int operator>( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) > 0 ) ;
}
int operator<=( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) <= 0 ) ;
}
int operator>=( const String& l, const String& r ) // comparison operator
{
return ( strcmp( l.rep, r.rep ) >= 0 ) ;
}
char& String:perator[]( int i ) { return rep ; } // access to component character
ostream& operator<<(ostream& os, const String& s)
{
return os << s.rep ;
}
istream& operator>>(istream& is, String& s)
{
char buffer[256] ;
is >> buffer ;
s = buffer ;
return is ;
}
String operator+( const String l, const String r )
{
char* buff = new char[l.length() + r.length() + 1] ;
strcpy( buff, l() ) ;
strcat( buff, r() ) ;
return String( buff ) ;
}