Need help, String Class
Feb 3, 2013 at 10:20pm UTC
Hi,
I have an assignment to make my own string class and to avoid using the c++ string class. I'm just about done, but I am getting the following error when I try to compile.
1 2 3 4 5 6 7
string_test.cpp: In function ‘int main()’:
string_test.cpp:23: error: no match for ‘operator =’ in ‘s3 = "bye" ’
string.h:26: note: candidates are: void String::operator =(String&)
string_test.cpp:29: error: no match for ‘operator +=’ in ‘s1 += "re" ’
string.h:27: note: candidates are: void String::operator +=(String&)
string_test.cpp:32: error: no match for ‘operator +=’ in ‘s1 += "d" ’
string.h:27: note: candidates are: void String::operator +=(String&)
Here's my code, I can't understand why it's not finding a match for those operators when I have them declared in my class code?
string.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#ifndef STRING_H
#define STRING_H
// String.h
#include <iostream>
using namespace std;
class String {
int size;
char * buffer;
public :
String();
String(const String & s);
String(const char * s);
~String();
//functions
int length();
void resize(unsigned int , char );
void insert(unsigned int , String&);
//operators
char & operator [](unsigned int );
void operator = (String &);
void operator += (String &);
friend bool operator ==(const String &, const String &);
friend bool operator <=(const String &, const String &);
friend bool operator <(const String &, const String &);
friend ostream & operator <<(ostream &, const String &);
};
#endif
string.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#include "string.h"
#include <cassert>
using namespace std;
String::String()
{
buffer = 0;
resize(0, ' ' );
}
String::String(const String& s)
{
size = s.size;
buffer = new char [size];
for (int i = 0; i < size; i++){
buffer[i] = s.buffer[i];
}
}
String::String(const char *p)
{
int p_size = 0;
for (const char *q = p; *q; q++){
p_size++;
}
buffer = new char [p_size];
for (int i=0;i<p_size;i++){
size = p_size;
}
}
String::~String()
{
delete [] buffer;
}
int String::length()
{
int i = 0;
while (buffer[i] != '\0' ){
i++;
}
return i;
}
void String::resize(unsigned int newlength, char pad)
{
if (buffer == 0){
size = 0;
}
if (newlength < size){
buffer[newlength = '\0' ];
}
else {
int i;
char * newbuffer = new char [newlength + 1];
assert (newbuffer != 0);
for (i=0; i < size && buffer[i] != '\0' ; i++){
newbuffer[i] = buffer[i];
}
for ( ; i < newlength;i++){
newbuffer[i] = pad;
}
newbuffer[i] = '\0' ;
if (buffer != 0){
delete [] buffer;
}
buffer = newbuffer;
size = newlength;
}
}
void String::insert(unsigned int position, String &text)
{
int lgt = length();
int addlength = text.length();
int newlength = lgt + addlength;
resize(newlength, '\0' );
for (int i = lgt; i >= position; i--){
buffer[i + addlength] = buffer[i];
}
for (int i = 0; i < addlength; i++){
buffer[position + i] = text[i];
}
}
char & String::operator [](unsigned int index)
{
assert (index <= size);
return buffer[index];
}
void String::operator = (String & p){
resize(p.length(), ' ' );
strcpy(buffer, p.buffer);
}
void String::operator += (String & s){
insert(length(), s);
}
bool operator ==(const String &S1, const String &S2)
{
if (S1.size == S2.size)
{
int counter1 = S1.size;
int counter2 = 0;
int i = 0;
while (i != counter1)
{
if (S1.buffer[i] != S2.buffer[i]){
counter2++;
}
i++;
}
if (counter2 != 0) {
return false ;
}
else {
return true ;
}
}
else {
return false ;
}
}
bool operator <=(const String &S1, const String &S2){
if (S1.size <= S2.size){
int counter1 = S1.size;
int counter2 = 0;
int i = 0;
while (i != counter1){
if (S1.buffer[i] <= S2.buffer[i]){
counter2++;
}
i++;
}
if (counter2 != 0){
return true ;
}
else {
return false ;
}
}
else {
return false ;
}
}
ostream& operator << ( ostream& os, const String& s)
{
os << s.size;
return os;
}
string_test.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "string.h"
#include <cassert>
#include <iostream>
using namespace std;
int main()
{
String s1; // s1 == ""
assert(s1.length() == 0);
String s2("hi" ); // s2 == "hi"
assert(s2.length() == 2);
String s3(s2); // s3 == "hi"
assert(s3.length() == 2);
assert(s3[0] == 'h' );
assert(s3[1] == 'i' );
s1 = s2; // s1 == "hi"
assert(s1 == s2);
s3 = "bye" ; // s3 == "bye"
assert(s3.length() == 3);
assert(s3[0] == 'b' );
assert(s3[1] == 'y' );
assert(s3[2] == 'e' );
s1 += "re" ; // s1 == "hire"
assert(s1 == "hire" );
s1 += "d" ; // s1 == "hired"
assert(not (s1 == "hire" ));
cout << "SUCCESS" << endl;
}
Thanks for your help!
Last edited on Feb 3, 2013 at 10:21pm UTC
Feb 3, 2013 at 10:29pm UTC
operator = (const String &);
Feb 3, 2013 at 10:36pm UTC
I've actually tried that, but it causes a second error when trying to compile string.cpp.
1 2
string.cpp: In member function ‘void String::operator =(const String&)’:
string.cpp:95: error: passing ‘const String’ as ‘this ’ argument of ‘int String::length()’ discards qualifiers
Feb 3, 2013 at 10:53pm UTC
check out `const correctness'
int length() const ;
Feb 3, 2013 at 10:53pm UTC
Alright got it to compile! However my second assertion failed... :(
1 2
Assertion failed: (s2.length() == 2), function main, file string_test.cpp, line 13.
Abort trap: 6
Now to find out what's wrong.
Feb 3, 2013 at 10:54pm UTC
Yeah I did that ne! Thanks! Found I was missing several const.
Topic archived. No new replies allowed.