Question: compiler required: visual studio 2019 and do not use #include I get these warnings and errors I do not know how to fix no matter
compiler required: visual studio 2019 and do not use #include
I get these warnings and errors I do not know how to fix no matter what. Without fixing this my program will not run. It keeps giving me a failed build. Rewrite the program
String.h------------------------------------------------------------
#ifndef STRING_H #define STRING_H
#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
INC.String.cpp---------------------------------------------------------------------
#include "String.h" #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
String::String(const String& right) { len = right.length(); buffer = new char[len + 1]; for (int i = 0; i
String::~String() { delete[] buffer; }
int String::length() const { return len; }
char& String::operator [] (int index) { assert((index >= 0) && (index
char String::operator [] (int index) const { assert((index >= 0) && (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
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
int String::compare(const String& data) { for (int i = 0; i len && i buffer[i]) > ((int)data[i])) { return 1; } else if (((int)this->buffer[i]) len > data.length()) { return 1; } if (this->len
ostream& operator
INC.String.test-------------------------------------------------------------------
#include "INC.String.cpp" #include
using namespace std;
int main() //stringtest { String new_name; cout
INC.String INC.String INC.String INC.String INC.String INC.String INC.String INC.String INC.String.cpp 89 INC.String.cpp 112 INC.String.exe 1 INC.String.test.obj 1 INC.String.test.obj 1 INC.String.test.obj 1 INC.String.test.obj 1 INC.String.test.obj 1 ! C6386 Buffer overrun while writing to 'buffer: the writable size is '(len+1)*1' bytes, but '3' bytes might be written. ! C6386 Buffer overrun while writing to 'buffer': the writable size is '(len+1)*1' bytes, but 'len' bytes might be written. LNK1169 one or more multiply defined symbols found XLNK2005 "public: _thiscall String::String(class String const &)" (??oString@@QAE@ABVO@@Z) already defined in INC.String.obj XLNK2005 "public: _thiscall String::String(char const* const)" (??OString@@QAE@QBD@Z) already defined in INC.String.obj LNK2005 "public: _thiscall String:String(void)" (??oString@@QAE@XZ) already defined in INC.String.obj LNK2005 "public: _thiscall String:-String(void)" (??1String@@QAE@XZ) already defined in INC.String.obj XLNK2005 "public class String & _thiscall String:operator=(class String const &)" (??4String@@QAEAAVO@ABVO@@Z) already defined in INC.String.obj "class std::basic_ostream
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
