String class implementation

For coding practice I am writing a String class based on something a professor gave us back when I took data structures. I am getting an error in my code and don't know how to fix it.

This is the erorr.

|104|error: passing 'const String' as 'this' argument of 'const int String::length()' discards qualifiers [-fpermissive]|

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
#include <iostream>
#include <cassert>

using namespace std;

class String {
        int size;
        char * buffer;
public:
        String();
        String(const String &);
        String(const char *);
        ~String();
        int length();
        void resize(unsigned int, char);
        void insert(unsigned int, String&);
        char& operator[] (unsigned int);
        void operator =(const String&);
        void operator += (const String&);

        	// other methods
        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 &);
};

String::String()
{
    buffer = nullptr;
    size = 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 i = 0;
    const char * t = p;

    while(*p++)
    {
        i++;
    }

    buffer = new char[i];
    int j = 0;

    for(j;*t;t++,j++)
    {
        buffer[j] = *t;
    }
    size = j;
}

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

int String::length()
{
    if(buffer == nullptr)
    {
        return 0;
    }
    else
    {
        return size;
    }
}

char & String::operator[] (unsigned int x)
{
    return buffer[x];
}

void String::operator =(const String&s)
{
    buffer = s.buffer;

}

ostream & operator<<(ostream &os, const String &s)
{
    for(int i = 0; i < s.size; i++)
    {
        os << s.buffer[i];
    }
    return os;

}

bool operator==(const String & s, const String & t)
{
    if(s.length() != t.length())
    {
        return false;
    }
    else
    {
        for(int i = 0; i < s.length(); i++)
        {
            if(s.buffer[i] != t.buffer[i])
            {
                return false;
            }
        }
    }
    return true;

}


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;
*/
}
Last edited on
Your length() function should be a const member function. Change line 14 to:

int length() const

and line 68 to:

int String::length() const

(The problem is that some of your operators take a const String and then call the length() function.)

I haven't look for any other errors.
Last edited on
For operator =, you need to copy the size as well
Thanks for the help. It's working now.
Topic archived. No new replies allowed.