I seem to be struggling with understanding this particular topic in my class. I was given an assignment with these rules:
Create a class named StringBuilder and split its code into two files: a header file named stringbuilder.h and an implementation file named stringbuilder.cpp. This class will be used to piece together larger strings from smaller ones. It should have a single private string data member called content. It must also define the following public members:
StringBuilder(): a public no-argument constructor that initializes content to an empty string.
StringBuilder(const string& str): a public constructor that takes a string argument and adds it to its content.
const string& str() const: returns its content.
This class must also overload the following three operators using member functions:
void operator*(unsigned int n): takes the value of content and add it to itself n times if n is > 1. It should have no impact If n <= 1. This allows you to write something like:
StringBuilder sb("foo"); // sb's content = "foo"
sb * 5; // Now sb's content = "foofoofoofoofoo"
bool operator==(StringBuilder& sb): used to compare two StringBuilder objects. It returns true if the contents of both builders are the same. This allows you to write something like:
StringBuilder sb1("One"), sb2("One");
if(sb1 == sb2){
cout << "The same";
}
bool operator!=(StringBuilder& sb): the opposite of operator== returning true of the contents of both builders are different.
Finally, this class must overload the following two operators using friend functions:
void operator>>(string str, StringBuilder& sb): used for adding a string to the content of a StringBuilder object by writing something like this:
StringBuilder sb;
"Hello" >> sb; // Now sb contains Hello
ostream& operator<<(ostream& out, StringBuilder& sb): used for printing the content of the StringBuilder object to the console by writing something like this:
StringBuilder sb;
cout << sb;
In a second .cpp file, write a main() function that tests this class and makes sure it runs as expected. This function must call every constructor/function/overloaded operator in this class.
I coded the class definition as stated here :
#ifndef _stringbuilder_h_
#define _stringbuilder_h
#include <iostream>
using namespace std;
#include <string>
#include <sstream>
class StringBuilder{
private:
string content;
public:
//Constructors
StringBuilder():content(""){}
StringBuilder(const string& str): content(str){}
const string& str() const{return content;}
void operator*(unsigned int n);
bool operator==(StringBuilder& sb);
bool operator!=(StringBuilder& sb);
friend void operator>>(string str, StringBuilder& sb);
friend ostream& operator<<(ostream& out, StringBuilder& sb);
};
# endif
Then I attempted to code it's related implementation as follows:
#include "stringbuilder.h"
//Member functions
void operator*(unsigned int n){
if (n>1){
return content+=content;
}
}
bool operator==(StringBuilder& sb){
if (sb==content){
return true;
}
}
bool operator!=(StringBuilder& sb){
if (sb != content){
return true;
}
}
friend void operator>>(string str, StringBuilder& sb){
cin>> str;
str = content + sb;
}
friend ostream& operator<<(ostream& out, StringBuilder& sb){
out<< sb::;
}
The current issues i'm facing now after moving the implementation files from my class definition are now that: Each operator is highlighted with the error - nonmember operator requires a parameter with class or enumeration type
I am unable to reference the value of content because it is a private member
Is there any guidance I could receive in accomplishing this?
Aucun commentaire:
Enregistrer un commentaire