Class that handles String implementation

I get the following errors:

IntelliSense: no operator "==" matches these operand types are: const String1030 == String1030

error C2676: binary '==' : 'const String1030' does not define this operator or a conversion to a type acceptable to the predefined operator


error C2572: 'String1030::String1030' : redefinition of default parameter : parameter 1

Here is String1030.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264



#ifndef STRING1030_H
#define STRING1030_H

#include<iostream>
using std::string; 
using std::ostream;
using std::istream;
using std::endl;
using std::cerr;


/*
 * The class String1030 is for the students to practice implementing
 * more class functions and making sure that their implementation
 * is reasonable.
 *
 * It requires some skill in using arrays and overloading operators.
 *
 * Note that the sentinel value that MUST be part of the storage for our
 * strings is '\0'.  That is not special, it is just a way to tell future
 * readers that we know what we are doing. We could just as well use the 
 * digit 0, but that can be very confusing.
 */


class String1030
{

  public:
      // The constructor. The "0" is the digit 0 NOT a 
      // character. It is used to let us know that 
      // nothing is being passed to the default constructor.
    String1030(const char *buf=0);
      //This next is a "copy" constructor. Remember that
      //we have to create new storage and then copy
      //the array content. We must not just copy the pointer.
    String1030(const String1030& oldstring);
      // The destructor is needed because we are allocating memory.
      // We must deallocate it when the object goes out of
      // scope (is destroyed).
    ~String1030();
      // Returns the number of characters stored excluding the
      // the sentinel value.
    int getSize(void) const;
      // Resizes the storage. Must include 1 extra space for the
      // sentinel value.
    void setSize(int newsize);


    String1030& operator=(const String1030& right);

	bool operator == (const String1030& right) const ;

      // Allows us to change the element at a certain index.
      // refer to the IntList code.
   int operator[]( int index ) const;

   char& operator[](int index);


      // Returns a pointer to array storing the string.
    const char *getString();
      // Replace the existing string with a new one. 
    void setString(const char *carray);


  private:
    char *buffer;
    int mysize;

};





// Basic constructor.
//
String1030 :: String1030(const char *buff=0): mysize(0), buffer(0)
{
  setSize(*buff);
}

//copy constructor
String1030::String1030(const String1030& oldstring) : mysize(0), buffer(0)
{
	if (oldstring.getSize() <= 0) {
		setSize(0);
	}
	else {
		setSize(oldstring.getSize());
		for (int i = 0; i< mysize; i++) {
			buffer[i] = oldstring.buffer[i];
		}
	}
}


// Destructor call
String1030::~String1030()
{
  setSize(0);
}


String1030& String1030:: operator=(const String1030& right){
		// must take 'address of' the argument to compare it to the
		// this pointer.
		if (&right == this) 
		{
			cerr << "Warning:  attempt to copy IntList onto self" << endl;
		 }
		else {
			if (right.getSize() +1 <= 0) {
				setSize(0);
			}
			else {
				setSize(right.getSize());
				for (int i = 0; i< mysize +1 ; i++) {
					buffer[i] = right.buffer[i];
				}
			}
		}
		return *this;  // dereference the pointer to get the object
	}



char& String1030::operator[](int index)
{
	if (index<0 || index >= mysize + 1) {
		cerr << "Attempt to access element outside index bounds"
			<< endl;
		exit(1); // Maybe not reasonable but what the heck.
	}
	else {
		return buffer[index];
	}
}


int String1030::getSize()  const
{
	return mysize;
}


void String1030::setSize(int newsize)
{
	if ( newsize <0) {
		cerr << "Warning:  attempt to set a size < 0" << endl;
		exit(2);  // is this reasonable?
	}
	else {
		if (buffer != 0) {
			delete[] buffer;
			buffer = 0;
			mysize = 0;
		}
		if (newsize != 0) {
			buffer = new char[newsize+1];
			if (buffer == 0) {
				mysize = 0;
				cerr << "Warning:  unable to allocate enough space for list" << endl;
				exit(3); 
			}
			else {
				mysize = newsize;
			}
		}
	}

}

// Returns a pointer to array storing the string.
const char* String1030:: getString()
{
	return    buffer;
}

// Replace the existing string with a new one. 
void  String1030::setString(const char *carray)
{
	
int len = 0;
for ( int tmp = 0; carray[tmp] != 0; tmp ++){
	len =  len + 1;
}

setSize(len);

for(int i=0; i < len +1; i++){
	buffer[i] = carray[i];
}

}

bool String1030::operator == ( const String1030& right ) const
{
  bool result=true;
  if( right !== right.getSize() ) {
    result=false;
  } else {
    for(int i=0; i< mysize && result == true; i++) {
      result = buffer[i]==right.buffer[i];
    }
  }
  return result;
}
ostream& operator<<( ostream& , const String1030& );
istream& operator>>( istream& , String1030& );



ostream& operator<<( ostream& L, const String1030& R)
{
  if( R.getSize() > 0 ) {
    L << endl;
    for( int i=0; i< R.getSize(); i++ ) {
      L << "[" << i << "] " << R[i] << endl;
    }
  } else {
    cerr << "Warning:  Attempt to display empty IntList" << endl;
  }
  return L;
}

// Note that this does NOT validate the input after the first value.
// That is intentional here. Your future code should be more robust.
//
istream& operator>>( istream& L, String1030& R)
{
  int tmp;
 L >> tmp;
  if( tmp<=0 ) {
    cerr << "First value expected to be list size, > 0." << endl;
  } else {
    if( tmp!=R.getSize() ) {
      R.setSize(tmp);
    }
    for (int i=0; i< R.getSize(); i++) {
      L >> R[i];
    }
  }
  return L;
}

int String1030::operator[]( int index ) const
{
  if( index<0 || index>=mysize ) {
    cerr << "Attempt to access element outside index bounds" 
      << endl;
    exit(4); // Maybe not reasonable. 
  } else {
    return buffer[index];
  }
}

#endif



Here is the other code:

Prog10.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
#include<iostream>
#include "String1030.h"

using std::cout;
using std::cin;

int main()
{
	

  // check out the use of the constructors 
  String1030 s("My string");
  String1030 t(s);
  String1030 x;

  char in_buf[256];


  cout << "S size(): " << s.getSize() << endl;
  cout << "T size(): " << t.getSize() << endl;
  cout << "X size(): " << x.getSize() << endl;

  for(int i=0;i<t.getSize();i++)
    cout << t[i];
  cout << endl;

  // check the ability to modify one element of the string
  s[2]='5';

  for(int i=0;i<s.getSize();i++)
    cout << s[i];
  cout << endl;

  // check the assignment operator
  x=s;
  cout << "X: " << x.getString() << endl;

  // check the size reset.
  x.setSize(30);

  cout << "Input a string: ";
  cin >> in_buf;
  x.setString(in_buf);
  cout <<  "\nX: " << x.getString() << endl;


  //more checks on resize

  //set to a negative value, nothing should change
  s.setSize(-8);
  cout << "S size(): " << s.getSize() << endl;

  //set to 0, should be 0
  s.setSize(0);
  cout << "S size(): " << s.getSize() << endl;

  //read into the 0 length array should NOT have an error
  //and should NOT transfer any characters. Output should not
  //have any errors either.
  cout << "Input a string: ";
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //reset to something larger than 0
  s.setSize(10);
  cout << "S size(): " << s.getSize() << endl;

  //read should work now
  cout << "Input a string: ";
  cin >> in_buf;
  s.setString(in_buf);
  cout << "S after cin>>: " << s.getString() << endl;

  //now the assignment return value

  x=t=s;

  cout << "T: " << t.getString() << endl;
  cout << "X: " << x.getString() << endl;


  return 0;

}
Last edited on
Topic archived. No new replies allowed.