Question: How can I fix this error for C++, below is the code and error. Thank you! #include #include using namespace std; class MyString { private:
How can I fix this error for C++, below is the code and error. Thank you!
#include
#include
using namespace std;
class MyString {
private:
char* myString;
int curr_length;
int arr_size;
//Ensure capacity to array
void ensureCapacity() {
arr_size += arr_size;
char* temp;
temp = new char[curr_length+1];
for (int i = 0; i
temp[i] = myString[i];
}
temp[curr_length] = '\0';
myString = new char[arr_size];
for (int i = 0; i
myString[i] = temp[i];
}
myString[curr_length] = '\0';
}
public:
//Default constructor
MyString() {
curr_length = 0;
arr_size = 0;
myString = NULL;
}
//Parameterized constructor
MyString(string str) {
arr_size = str.length()+1;
myString = new char[arr_size];
curr_length = 0;
for (int i = 0; i
myString[i] = str[i];
curr_length++;
}
myString[curr_length] = '\0';
}
//Copy constructor
MyString(const MyString& str) {
arr_size = str.arr_size;
myString = new char[arr_size];
curr_length = 0;
for (int i = 0; i myString[i] = str.myString[i]; curr_length++; } myString[curr_length] = '\0'; } //Return length of the string int getLength() { return curr_length; } //Extraction operator overload friend ostream& operator for (int i = 0; i out } return out; } //Cocatenation MyString& operator+(const MyString& str) { int capacity = str.arr_size + this->arr_size; while (this->arr_size this->ensureCapacity(); } for (int i = 0; i this->myString[curr_length++] = str.myString[i]; } this->myString[curr_length] = '\0'; return *this; } bool operator==(const MyString& str) { if (str.curr_length == curr_length) { for (int i = 0; i if (this->myString[i] != str.myString[i]) { return 0; } } return true; } return false; } //Compare two strings int compareTo(const MyString& str) { if (strcmp(this->myString, str.myString) return -1; } else if (strcmp(this->myString, str.myString) > 0) { return 1; } else { return 0; } } //method that takes an integer and returns the character at that index location. char get(int index) { if (index return myString[index]; } return ' '; } //Uppercase a string void toUpper() { for (int i = 0; i myString[i] = toupper(myString[i]); } } //Lowercase a string void toLower() { for (int i = 0; i myString[i] = tolower(myString[i]); } } //that takes an integer and returns the substring starting at that index. char* substring(int index) { char * temp; if (index temp = new char[curr_length + 1]; int i=0,j = 0; for (i = index; i temp[j++] = myString[i]; } temp[j] = '\0'; } return temp;; } //Return a MyString substring where n is the starting index and m is one past the ending index. char* substring(int n,int m) { char * temp; temp = new char[curr_length + 1]; if (m int i = 0, j = 0; for (i =n; i temp[j++] = myString[i]; } temp[j] = '\0'; } return temp;; } int indexOf(const MyString& str) { char* temp; for (int i = 0; i if (str.myString[0] == myString[i]) { temp= this->substring(i, i+str.curr_length-1); cout if (strcmp(str.myString, temp) == 0) { return i; } } } return -1; } int lastIndexOf(const MyString& str) { char* temp; for (int i = curr_length-1;i>=0; i--) { if (str.myString[0] == myString[i]) { temp= this->substring(i,i+str.curr_length-1); if (strcmp(str.myString, temp) == 0) { return i; } } } return -1; } }; int main() { //Create 2 objects MyString str1("Hello world!"); MyString str2("Good Morning!!"); //Display strings cout cout //Concatenate and display str1 = str1 + str2; cout //Assignment test MyString str3 = str1; cout //Comparison test cout cout //Case changing test str2.toUpper(); cout str1.toLower(); cout //Index test str3.toUpper(); cout cout return 0; } 
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
