Oct 29, 2009 at 8:53pm UTC
Hi, when I compile my code for proj07.string.cpp, i receive the following errors:
proj07.string.cpp:31: error: 'String& operator=(const String&)' must be a nonstatic member function
proj07.string.cpp:42: error: 'String& operator+=(const String&)' must take exactly two arguments
I'm not sure how to overcome either error, any suggestions?
Note: I'm not allowed to change the header file, so any editing must be done on proj07.string.cpp
*********************************************************************************
//proj07.string.h
#ifndef STRING_
#define STRING_
using namespace std;
#include <iostream>
class String
{
private:
// Capacity of a string
//
static const unsigned int MAX = 64;
char Mem[MAX]; // Memory to hold characters in string
unsigned Len; // Number of characters in string
public:
// Construct empty string
//
String() { Len = 0; }
// Reset string to empty
//
void reset() { Len = 0; }
// Return status information
//
bool empty() const { return Len == 0; }
bool full() const { return Len == MAX; }
unsigned length() const { return Len; }
unsigned maximum() const { return MAX; }
// Return reference to element I
//
char& operator[]( unsigned I ) { return Mem[I]; }
// Return constant reference to element I
//
const char& operator[]( unsigned I ) const { return Mem[I]; }
// Construct string by copying existing string
//
String( const String& );
// Construct string by copying array of characters
//
String( const char [] );
// Copy string to the current string
//
String& operator=( const String& );
// Append string to the current string
//
String& operator+=( const String& );
};
// Return string which is the concatenation of two strings
//
String operator+( const String&, const String& );
// Compare two strings
//
bool operator==( const String&, const String& );
bool operator< ( const String&, const String& );
// Output string to stream
//
ostream& operator<<( ostream&, const String& );
// Input string from stream
//
istream& operator>>( istream&, String& );
#endif
**************************************************************************
//proj07.string.cpp
#include <iomanip>
#include <iostream>
#include "project07.string.h"
#include <string.h>
using namespace std;
String::String( const String& Copy)
{
int i;
Len = Copy.length();
for (i = 0; i <= Copy.length(); i++)
{
Mem[i] = Copy.Mem[i];
}
Mem[i] = NULL;
}
String::String( const char Array[])
{
int i;
for (i = 0; i < strlen(Array); i++)
{
Mem[i] = Array[i];
}
Len = i;
}
String::String& operator=( const String& Copy)
{
int i;
Len = Copy.length();
for (i = 0; i < Copy.length(); i++)
{
Mem[i] = Copy[i];
}
Mem[i] = '\0';
}
String::String& operator+=( const String& Copy)
{
int i;
for (i = 0; i < Copy.length(); i++)
{
Mem[Len] = Copy[i];
Len++;
}
}
String operator+(const String& One, const String& Two)
{
char Concant [] = {};
int i = 0;
int i2 = 0;
while (i < One.length())
{
Concant[i] = One[i];
i++;
}
while (i2 < Two.length())
{
Concant[i] = Two[i2];
i++;
i2++;
}
return Concant;
}
bool operator==(const String& One, const String& Two)
{
bool Val = true;
for (int i = 0; i < One.length(); i++)
{
if (One[i] != Two[i])
{
Val = false;
}
}
return Val;
}
bool operator<(const String& One, const String& Two )
{
if (One.length() < Two.length())
{
return true;
}
else
{
return false;
}
}
ostream& operator<<( ostream& Stream, const String& One)
{
int i;
for (i = 0; i < One.length(); i++)
{
Stream << One[i];
}
return Stream;
}
istream& operator>>(istream& Stream, String& One)
{
char InputString [] = {};
Stream >> InputString;
int i;
for (i = 0; i < strlen(InputString); i++)
{
One[i] = InputString[i];
}
return Stream;
}
Oct 29, 2009 at 8:59pm UTC
You're defining the function wrong in the .cpp.
It's not
String::String& operator =( const String& Copy){
It's
String& String::operator =( const String& Copy){
The same goes for all the other functions. They're all missing the scope.