Well, i thought everything was perfect since my last post. But i have the strangest error. Maybe its because i don't completely understand references... So anyway here is my string class:
class StRiNg
{
public:
StRiNg();
StRiNg(const char* const);
StRiNg(const StRiNg&);
~StRiNg();
StRiNg& operator=(const StRiNg&);
char &operator[](int);
char operator[](int) const;
StRiNg operator+(const StRiNg&);
void operator+=(const StRiNg&);
friend ostream& operator<<(ostream&,StRiNg&);
bool operator==(const StRiNg&);
bool operator!=(const StRiNg&);
int GetLength() { return sTrLeN; }
const char* GetString() const { return sTr; }
private:
StRiNg(int);
char* sTr;
int sTrLeN;
};
// String Class Defined
StRiNg::StRiNg()
{
sTrLeN = 0;
sTr = new char[1];
sTr[0] = '\0';
}
StRiNg::StRiNg(int len)
{
sTr = new char[len+1];
for(int i = 0; i <= len; i++)
sTr = '\0';
sTrLeN = len;
}
StRiNg::StRiNg(const char* const s)
{
if(s == 0)
{
sTrLeN = 0;
sTr = new char[1];
sTr[0] = '\0';
}
else
{
sTrLeN = (int)strlen(s);
sTr = new char[sTrLeN+1];
for(int i = 0; i < sTrLeN; i++)
sTr[i] = s[i];
sTr[sTrLeN] = '\0';
}
}
StRiNg::StRiNg(const StRiNg& s)
{
sTrLeN = s.GetLength();
sTr = new char[sTrLeN+1];
for(int i = 0; i < sTrLeN; i++)
sTr[i] = s[i];
sTr[sTrLeN] = '\0';
}
StRiNg::~StRiNg()
{
delete [] sTr;
sTrLeN = 0;
}
StRiNg& StRiNg::operator=(const StRiNg& s)
{
if(this == &s)
return *this;
delete [] sTr;
sTrLeN = s.GetLength();
sTr = new char[sTrLeN+1];
for(int i = 0; i < sTrLeN; i++)
sTr[i] = s[i];
sTr[sTrLeN] = '\0';
return *this;
}
char& StRiNg::operator[](int offset)
{
if(offset > sTrLeN)
return sTr[sTrLeN-1];
else if(offset >= 0)
return sTr[offset];
else
return sTr[0];
}
char StRiNg::operator[](int offset) const
{
if(offset > sTrLeN)
return sTr[sTrLeN-1];
else if(offset >= 0)
return sTr[offset];
else
return sTr[0];
}
StRiNg StRiNg::operator+(const StRiNg& s)
{
int totLen = sTrLeN + s.GetLength();
StRiNg temp(totLen);
int i,j;
for(i = 0; i < sTrLeN; i++)
temp[i] = sTr[i];
for(j = 0; j < s.GetLength(); j++,i++)
temp[i] = s[i];
temp[totLen] = '\0';
return temp;
}
void StRiNg::operator+=(const StRiNg& s)
{
int len = s.GetLength();
int totLen = sTrLeN + len;
StRiNg temp(totLen);
int i,j;
for(i = 0; i < sTrLeN; i++)
temp[i] = sTr[i];
for(j = 0; j < s.GetLength(); j++,i++)
temp[i] = s[i-sTrLeN];
temp[totLen] = '\0';
*this = temp;
}
bool StRiNg::operator==(const StRiNg& s)
{
if(s.GetLength() != sTrLeN)
return false;
bool same = true;
for(int i = 0; i < sTrLeN && same; i++)
{
if(sTr[i] != s[i])
same = false;
}
if(!same)
return false;
return true;
}
bool StRiNg::operator!=(const StRiNg& s)
{
if(*this == s)
return false;
return true;
}
// For cout <<
ostream& operator<<(ostream& theStream,StRiNg& theString)
{
theStream << theString.sTr;
return theStream;
}
the errors I'm getting are where i define a StRiNg& object and attempt to do the GetLength() function:
1>------ Build started: Project: String Class, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\daniel\documents\visual studio 2008\projects\string class\string class\string.h(67) : error C2662: 'String::StRiNg::GetLength' : cannot convert 'this' pointer from 'const String::StRiNg' to 'String::StRiNg &'
1> Conversion loses qualifiers
1>c:\users\daniel\documents\visual studio 2008\projects\string class\string class\string.h(83) : error C2662: 'String::StRiNg::GetLength' : cannot convert 'this' pointer from 'const String::StRiNg' to 'String::StRiNg &'
1> Conversion loses qualifiers
1>c:\users\daniel\documents\visual studio 2008\projects\string class\string class\string.h(110) : error C2662: 'String::StRiNg::GetLength' : cannot convert 'this' pointer from 'const String::StRiNg' to 'String::StRiNg &'
1> Conversion loses qualifiers
1>c:\users\daniel\documents\visual studio 2008\projects\string class\string class\string.h(115) : error C2662: 'String::StRiNg::GetLength' : cannot convert 'this' pointer from 'const String::StRiNg' to 'String::StRiNg &'
1> Conversion loses qualifiers
1>c:\users\daniel\documents\visual studio 2008\projects\string class\string class\string.h(122) : error C2662: 'String::StRiNg::GetLength' : cannot convert 'this' pointer from 'const String::StRiNg' to 'String::StRiNg &'
1> Conversion loses qualifiers
1>c:\users\daniel\documents\visual studio 2008\projects\string class\string class\string.h(128) : error C2662: 'String::StRiNg::GetLength' : cannot convert 'this' pointer from 'const String::StRiNg' to 'String::StRiNg &'
1> Conversion loses qualifiers
1>c:\users\daniel\documents\visual studio 2008\projects\string class\string class\string.h(135) : error C2662: 'String::StRiNg::GetLength' : cannot convert 'this' pointer from 'const String::StRiNg' to 'String::StRiNg &'
1> Conversion loses qualifiers
1>Build log was saved at "file://c:\Users\Daniel\Documents\Visual Studio 2008\Projects\String Class\String Class\Debug\BuildLog.htm"
1>String Class - 7 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm making it in Microsoft Visual Studio 2008 Express Edition if that will do anything..
Thanks!
BTW!
In the errors it says: String::StRiNg because its in a namespace named String.