Overloading operator '<' problem

Hi,

First time posting here. I have gained a lot from this website.

I need a little help with a homework assignment. I have created a String class to handle basic string operations (Copy, Concat, Compare, Display, & Length). We are adding overloaded operators to the class and in particlular I am having trouble with the < operator. What I have done thus far works with a string on the lhs and a string on the rhs. It also works with a string object on the lhs and a string object on the rhs. Where I am having trouble is if I have a string on the lhs and a string object on the rhs. I get the error:

error C2679: binary '<' : no operator found which takes a right-hand operand of type 'String' (or there is no acceptable conversion)
1> could be 'built-in C++ operator<(const char [5], const char [5])'
1> while trying to match the argument list '(const char [5], String)'

Can someone please clue me in to what I am missing? The problem is refering to two lines in main.cpp near the end of the program. Thanks in advance.

--------String.h--------
#ifndef STRING_H
#define STRING_H

#include <cstring>
#include <iostream>

using namespace std;

class String
{
public:
String ();
String (const String &);
explicit String (const char []);
~String ();
int Compare (const String &) const;
int Compare (const char []) const;
String & Concat (const String &);
String & Concat (const char []);
String & Copy (const String &);
String & Copy (const char []);
void Display (ostream &) const;
size_t Length () const;
const String & operator = (const String &);
const String & operator = (const char []);
bool operator < (const String &);
bool operator < (const char []);

private:
char * pChar;
char * pCharTmp;
size_t NumChars;
size_t MaxChars;
};

inline int String::Compare(const String & S) const
{
return strcmp(pChar, S.pChar);
}

inline int String::Compare(const char Char []) const
{
return strcmp(pChar, Char);
}

inline void String::Display (ostream & out) const
{
out << pChar;
}

inline size_t String::Length () const
{
return NumChars;
}

#endif

----------String.cpp----------
#include "String.h"
#include <string.h>

#pragma warning (disable:4996)

String::String()
{
NumChars = 0;
MaxChars = 0;
pChar = new char [1];
pChar [0] = '\0';
}

String::String(const String & S)
{
NumChars = S.NumChars;
MaxChars = S.NumChars;
pChar = new char [S.NumChars + 1];
strcpy(pChar,S.pChar);
}

String::String(const char Chars [])
{
NumChars = strlen(Chars);
MaxChars = NumChars;
pChar = new char [NumChars + 1];
strcpy(pChar, Chars);
}

String::~String()
{
delete [] pChar;
}

String & String::Concat(const String & S)
{
if (MaxChars < (NumChars + S.NumChars))
{
cout << "MaxChars < NumChars + S.NumChars" << endl;
pCharTmp = new char [NumChars + S.Length() + 1];
strcpy(pCharTmp,pChar);
strcat(pCharTmp,S.pChar);
delete [] pChar;
pChar = new char [NumChars + S.Length() + 1];
MaxChars = (NumChars + S.Length());
strcpy(pChar,pCharTmp);
NumChars = MaxChars;
delete [] pCharTmp;
}
else if (MaxChars >= (NumChars + S.NumChars))
{
cout << "MaxChars >= NumChars + S.NumChars" << endl;
strcat(pChar,S.pChar);
NumChars = (NumChars + S.Length());
}
else;
return *this;
}

String & String::Concat(const char Char [])
{
if (MaxChars < (NumChars + strlen(Char)))
{
cout << "2MaxChars < NumChars + S.NumChars" << endl;
pCharTmp = new char [NumChars + strlen(Char) + 1];
strcpy(pCharTmp,pChar);
strcat(pCharTmp,Char);
delete [] pChar;
pChar = new char [NumChars + strlen(Char) + 1];
MaxChars = (NumChars + strlen(Char));
strcpy(pChar,pCharTmp);
NumChars = MaxChars;
delete [] pCharTmp;
}
else if (MaxChars >= (NumChars + strlen(Char)))
{
cout << "2MaxChars >= NumChars + S.NumChars" << endl;
strcat(pChar,Char);
NumChars = (NumChars + strlen(Char));
}
else;
return *this;
}

String & String::Copy(const String & S)
{
NumChars = S.NumChars;
if (MaxChars < S.NumChars)
{
delete [] pChar;
MaxChars = NumChars;
pChar = new char [NumChars + 1];
}
else;
strcpy(pChar, S.pChar);
return *this;
}

String & String::Copy(const char Char [])
{
NumChars = strlen(Char);
if (MaxChars < strlen(Char))
{
delete [] pChar;
MaxChars = NumChars;
pChar = new char [NumChars + 1];
}
else;
strcpy(pChar,Char);
return *this;
}

const String & String::operator = (const String & S)
{
NumChars = S.NumChars;
if (MaxChars < S.NumChars)
{
delete [] pChar;
MaxChars = NumChars;
pChar = new char [NumChars + 1];
}
else;
strcpy(pChar, S.pChar);
return *this;
}

const String & String::operator = (const char Char [])
{
NumChars = strlen(Char);
if (MaxChars < strlen(Char))
{
delete [] pChar;
MaxChars = NumChars;
pChar = new char [NumChars + 1];
}
else;
strcpy(pChar,Char);
return *this;
}

bool String::operator < (const String & S)
{
if (strcmp(pChar,S.pChar) < 0)
return true;
else
return false;
}

bool String::operator <(const char Char [])
{
if (strcmp(pChar,Char) < 0)
return true;
else
return false;
}

---------Main.cpp----------
#include <iostream>
#include <string.h>
#include "String.h"

using namespace std;

void main()
{
String S1;
String S2;

S1 = "abcd";
S2 = "efgh";
if (S1 < S2)
cout << "abcd occurs before efgh" << endl;
else if (S2 < S1)
cout << "efgh occurs before abcd" << endl;
else;

if (S1 < "efgh")
cout << "abcd occurs before efgh" << endl;
//error occurs here.
else if ("efgh" < S1)
cout << "efgh occurs before abcd" << endl;
else;

//error occurs here.
if ("abcd" < S2)
cout << "abcd occurs before efgh" << endl;
else if (S2 < "abcd")
cout << "efgh occurs before abcd" << endl;
else;
}









As your compiler says, you haven't provided an overloaded < operator suitable for this case.

This -> bool operator < (const char []); won't work, because the order of the arguments doesn't match.

This -> bool operator < (const String &); won't work, because of this -> explicit String (const char []);
Since you don't allow implicit casts, your compiler isn't allowed to turn "efgh" or "abcd" into Strings and then call the appropriate overload.
either

A possible solution is to also provide something like this (not as a member function):

bool operator < (const char cstr[], const String & str) {/*...*/}
Last edited on
Thank You m4sterr0shi!

I understood the problem, but not the solution. I discussed this with my instructor as well and his solution is exactly the same as yours. The final complete solution was

inline bool operator < (const char C [], const String & S)
{
return (S > C);
}

Of course the I had to define the > operator to make it all work.

Thanks Again!
Topic archived. No new replies allowed.