Question: compiler required: visual studio 2019 and do not use #include for some reason when my string test program is separated from my main string program,

compiler required: visual studio 2019 and do not use #include

for some reason when my string test program is separated from my main string program, there are many errors, warnings, and many messages that appear. This is in visual studio compiler and my professor does not use a different compiler. It works fine when the test program is in the main program but for some reason when it is separated it does not work. My professor requires a separate test program which is why I cannot just combine it.

string.cpp----------------------------------------------------------------------

#include "String.h" #include #include

using namespace std;

String::String() { len = 0; buffer = NULL; // No need to allocate array to hold zero characters }

String::String(const char p[]) { // Determine number of characters in string (strlen(p)) len = 0; while (p[len] != '\0') len++; // Allocate buffer array, remember to make space for NULL character buffer = new char[len + 1]; // Copy new characters (strcpy( buffer, p )) for (int i = 0; i < len; i++) buffer[i] = p[i]; buffer[len] = '\0'; }

String::String(const String& right) { len = right.length(); buffer = new char[len + 1]; for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; }

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

int String::length() const { return len; }

char& String::operator [] (int index) { assert((index >= 0) && (index < len)); return buffer[index]; }

char String::operator [] (int index) const { assert((index >= 0) && (index < len)); return buffer[index]; }

// + opearator is overloaded for concatenation using the += operator

String String::operator+(String& right) { return *this += right; }

//+= operator is overloaded for concatenation

String String::operator +=(String& s2) { int l1 = len; int l2 = s2.length(); char* temp = new char[len + 1]; for (int i = 0; i < len; i++) { temp[i] = buffer[i]; } temp[len] = '\0'; delete[] buffer; len = l1 + l2; buffer = new char[len + 1]; for (int i = 0; i < l1; i++) { buffer[i] = temp[i]; } for (int i = 0; i < l2; i++) { buffer[l1 + i] = s2[i]; }

buffer[len] = '\0';

return *this; }

// = operator is optimized for assignment and allocates new space only when the other string length is more than the current buffer

String& String::operator=(const String& right) { if (this != &right) { if (right.length() > len) { delete[] buffer; // Get rid of old buffer len = right.length(); buffer = new char[len + 1]; } len = right.length(); for (int i = 0; i < len; i++) buffer[i] = right[i]; buffer[len] = '\0'; } return *this; }

int String::compare(const String& data) { for (int i = 0; i < this->len && i < data.length(); i++) { if (((int)this->buffer[i]) > ((int)data[i])) { return 1; } else if (((int)this->buffer[i]) < ((int)data[i])) { return -1; } else { continue; } } if (this->len > data.length()) { return 1; } if (this->len < data.length()) { return -1; } return 0; }

ostream& operator << (ostream& out, const String& right) { int n = right.length(); for (int i = 0; i < n; i++) out << right[i]; return out; }

String.h-------------------------------------------------------------------------------

#ifndef STRING_H #define STRING_H

#include #include

class String { public: String(); // Default constructor String(const char p[]); // Simple constructor String(const String& right); // Copy constructor ~String(); // Destructor String& operator=(const String& right); // Assignment operator String& operator+=(const String& right); int length() const; char& operator[](int index); char operator[](int index) const; int compare(const String& data);//compare function implemented String operator+=(String& right);//+= operator overloaded String operator+(String& right);//+ operator overloaded

private: char* buffer; int len; };

#endif

stringtest.cpp-------------------------------------------------------

#include "String.h" #include #include

using namespace std;

int main() //stringtest { String new_name; cout << new_name << endl; char letters[] = "Hello World"; String s(letters); cout << s << endl; String first("Harry"); cout << first << endl; String second(first); cout << second << endl; String third = String("Two"); // Also uses copy constructor cout << third << endl; String classmates[5] = { "Draco", "Cedric" }; for (int i = 0; i < 5; i++) cout << i << " " << classmates[i] << endl; String test = classmates[2]; cout << "test is " << test << endl; cout << test.length() << endl; String a("Hermoine"); String b("Dumbledore"); String c = a + b;//using the + operator for concatenation cout << c << endl; cout << b.compare(a) << endl; String d("The "); d += c;//using the += operator for concatenation cout << d << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!