0

I just need a little help on this assignment. I have to redefine operators to work with strings. I'm starting with the == operator and I have it declared in my header file, however when I go to define the function in my cpp file, it says it's incompatible with the declared function. It's probably a stupid mistake, I just don't understand this sometimes.

string.h header file

    #pragma once

    #include <iostream>
    #include <string>
    using namespace std;

    #define NOT_FOUND -1

   // C++ String class that encapsulates an ASCII C-string
class String
{
 public:
// Default constructor
String();

// MUST HAVE: Copy-constructor that performs deep copy
String(const String& source);

// Init-constructor to initialize this String with a C-string
String(const char* text);

// Init constructor, allocates this String to hold the size characters
String(int size);

// Destructor
~String();

bool& compareTo(const String& cmp1);
// Assignment operator to perform deep copy
String& operator = (const String& source);

// Assignment operator to assign a C-string to this String
String& operator = (const char* text);

// Returns a reference to a single character from this String
char& operator [] (int index) const;

// Comparison operators
bool operator == (const String& compareTo) const;

string.cpp file

    #include "string.h"
#include <string>
#include <sstream>

using namespace std;

// Default constructor
String::String()
{
Text = NULL;
}

// MUST HAVE: Copy-constructor that performs deep copy
String::String(const String& source)
{
Text = NULL;
// Call the assignment operator to perform deep copy
*this = source;
}

// Init-constructor to initialize this String with a C-string
String::String(const char* text)
{
Text = NULL;
// Call the assignment operator to perform deep copy
*this = text;
}

// Init constructor, allocates this String to hold the size characters
String::String(int size)
{
Text = new char[size];
}

// Destructor
String::~String()
{
delete[] Text;
}

// Assignment operator to perform deep copy
String& String::operator = (const String& source)
{  
// Call the other assigment operator to perform deep copy
*this = source.Text;
return *this;
}

// Assignment operator to assign a C-string to this String
String& String::operator = (const char* text)
{
// Ddispose of old Text
delete[] Text;

// +1 accounts for NULL-terminator
int trueLength = GetLength(text) + 1;

// Dynamically allocate characters on heap
Text = new char[trueLength];

// Copy all characters from source to Text; +1 accounts for NULL-terminator
for ( int i = 0; i < trueLength; i++ )
    Text[i] = text[i];

return *this;
}

***bool& String::operator ==(string cmp2)***
{
};
Bobby Tables
  • 191
  • 2
  • 15
  • It's best to include the error that the compiler gives you. Literally. In most cases it's really recognizable to people familiar with the problem and can point them in the right direction faster. – TaZ Feb 13 '13 at 00:18
  • Avoid `using namespace anything` in source files. NEVER put it in headers. You wouldn't believe how annoying it can be if names like "sum" and "min" are already taken. With completely different meanings than you intended. – Cubic Feb 13 '13 at 00:18
  • Why do you want to return a `bool` as reference? If you call another function after you called `operator==` the variable might be already cleared until you check it. Also `bool`s size is on most platforms 8 bit while a reference is internally a pointer and it's size is either 32 or 64 bit (depending for what CPU you are building). This means you have a minimum overhead of 24 bit. – bash0r Feb 13 '13 at 00:25

1 Answers1

2

Your compareTo declaration has const while definition has no const, which means they have definition has different signature with declaration:

bool& compareTo(const String& cmp1);
                ^^^
bool& String::compareTo(string cmp2)
{
};

BTW, why does your compareTo return bool& ?

Also should avoid using namespace std; in any header files. see why-is-using-namespace-std-considered-a-bad-practice-in-c

Community
  • 1
  • 1
billz
  • 44,644
  • 9
  • 83
  • 100