I was given an exercise on overloading operators by my tutor. He gave me the int main() function which cannot be changed. I was supposed to write the functions etc so that the code would work. Unfortunately I have a seg fault. I've noticed that if the
TSeries series4=series1(2,4);
cout << "Series4: " << series4 << endl;
lines are commented it's more or less working. I would be very grateful for your help.
#include <iostream>
class TSeries {
public:
TSeries()
{
_size = 0;
_capacity = 0;
_tab = NULL;
}
TSeries(float *tab, const int size)
{
_tab = new float[size];
for (int i = 0; i < size; i++) _tab[i] = tab[i];
_size = size;
_capacity = 0;
}
~TSeries() { delete[] _tab; }
TSeries & operator+=(float value) { return insert(value); }
TSeries & operator,(float value) { return insert(value); }
TSeries & operator+(const TSeries & s)
{
// if(this->_size != s._size) std::cout<<"Size doesn't match!"<<std::endl;
/*else
{
std::cout<<"whee";
for(int i; i<this->_size;i++)
{
//this->_tab[i] += s._tab[i];
std::cout<<"nothing";
}
return *this;
}*/
//std::cout<<"sth";
}
TSeries & operator()(int position1, int position2)
{
// return *this;
}
TSeries & insert(float k)
{
if (_size >= _capacity) Enlarge();
_tab[_size++] = k;
return *this;
}
friend std::ostream & operator<<(std::ostream & out, const TSeries & s);
private:
int _size, _capacity;
float *_tab, *_itr;
static int _nr;
void Enlarge()
{
_capacity = 2 * _capacity + 1;
float *tmp = new float[_capacity];
for (int i = 0; i < _size; ++i)
{
tmp[i] = _tab[i];
}
delete[] _tab;
_tab = tmp;
}
};
std::ostream & operator<<(std::ostream & out, const TSeries & s)
{
int przedostatni = s._size - 1;
out << "(";
for (int i = 0; i < s._size; i++)
{
out << (int)s._tab[i];
if (i != przedostatni)
out << ",";
}
out << ")" << std::endl;
}
using namespace std;
int main(int argc, char **argv) {
TSeries series1;
series1 += 1., 2., 4., 2.;
cout << "Series1: " << series1 << endl;
const int size = 7;
float tab[size] = { 3.,3.,3.,4.,5.,1.,0. };
const TSeries series2(tab, size);
cout << "Series2: " << series2 << endl << endl;
TSeries series3 = series1 + series2;
cout << "Series3: " << series3 << endl << endl;
series1 += 1., 0., 3.;
series3 = series1 + series2;
cout << " " << series1 << endl;
cout << " +" << series2 << endl;
cout << " ---------------------" << endl;
cout << "Series3: " << series3 << endl << endl;
TSeries series4 = series1(2, 4);
cout << "Series4: " << series4 << endl;
return 0;
}
/* output required:
Series1: (1,2,4,2)
Series2: (3,3,3,4,5,1,0)
Size doesn't match!
Series3: ()
(1,2,4,2,1,0,3)
+(3,3,3,4,5,1,0)
---------------------
Series3: (4,5,7,6,6,1,3)
Series4: (4,2)
*/
std::ostream & operator<<(std::ostream & out, const TSeries & s) doesn't return anything. Please add a return out at the end of the function
operator() and operator+ should both end with return *this
As Devolus already pointed out: You don't have a copy constructor and no operator=(const TSeries&) defined
You can use memcpy to copy the arrays faster.#
The i in the for-loop in operator+(const TSeries&) isn't initialized.
Your operator(int, int) does currently alter the original object. This doesn't seem right.
Code:
#include <iostream>
class TSeries {
public:
TSeries()
{
_size = 0;
_capacity = 0;
_tab = NULL;
}
TSeries(float *tab, const int size)
{
_size = size;
_capacity = 0;
_tab = new float[size];
memcpy(_tab, tab, _size*sizeof(float));
}
TSeries(const TSeries& other)
{
_size = other._size;
_capacity = other._capacity;
_tab = new float[_size];
memcpy(_tab, other._tab, _size*sizeof(float));
}
~TSeries()
{
delete[] _tab;
}
TSeries & operator+=(float value) { return insert(value); }
TSeries & operator,(float value) { return insert(value); }
TSeries & operator+(const TSeries & other)
{
if (this->_size != other._size)
{
std::cout << "Size doesn't match!" << std::endl;
}
else
{
//std::cout << "whee";
for (int i = 0; i < this->_size; i++)
{
_tab[i] += other._tab[i];
//std::cout << "nothing";
}
}
//std::cout<<"sth";
return *this;
}
TSeries& operator=(const TSeries& other)
{
_size = other._size;
_capacity = other._capacity;
//Create tmp in case of self-assignment
float *tmp = new float[_capacity];
memcpy(tmp, other._tab, _size*sizeof(float));
delete[] _tab;
_tab = tmp;
return *this;
}
TSeries operator()(int position1, int position2)
{
//TODO: Range-Check
return TSeries(_tab + position1, position2 - position1);
}
TSeries & insert(float k)
{
if (_size >= _capacity) Enlarge();
_tab[_size++] = k;
return *this;
}
friend std::ostream & operator<<(std::ostream & out, const TSeries & s);
private:
int _size, _capacity;
float *_tab, *_itr;
static int _nr;
void Enlarge()
{
_capacity = 2 * _capacity + 1;
float *tmp = new float[_capacity];
memcpy(tmp, _tab, _size*sizeof(float));
delete[] _tab;
_tab = tmp;
}
};
std::ostream & operator<<(std::ostream & out, const TSeries & s)
{
int przedostatni = s._size - 1;
out << "(";
for (int i = 0; i < s._size; i++)
{
out << (int)s._tab[i];
if (i != przedostatni)
out << ",";
}
out << ")" << std::endl;
return out;
}
using namespace std;
int main(int argc, char **argv) {
TSeries series1;
series1 += 1., 2., 4., 2.;
cout << "Series1: " << series1 << endl;
const int size = 7;
float tab[size] = { 3.,3.,3.,4.,5.,1.,0. };
const TSeries series2(tab, size);
cout << "Series2: " << series2 << endl << endl;
TSeries series3 = series1 + series2;
cout << "Series3: " << series3 << endl << endl;
series1 += 1., 0., 3.;
series3 = series1 + series2;
cout << " " << series1 << endl;
cout << " +" << series2 << endl;
cout << " ---------------------" << endl;
cout << "Series3: " << series3 << endl << endl;
TSeries series4 = series1(2, 4);
cout << "Series4: " << series4 << endl;
return 0;
}
/* output required:
Series1: (1,2,4,2)
Series2: (3,3,3,4,5,1,0)
Size doesn't match!
Series3: ()
(1,2,4,2,1,0,3)
+(3,3,3,4,5,1,0)
---------------------
Series3: (4,5,7,6,6,1,3)
Series4: (4,2)
*/
Output
Series1: (1,2,4,2)
Series2: (3,3,3,4,5,1,0)
Size doesn't match!
Series3: (1,2,4,2)
(4,5,7,6,6,1,3)
+(3,3,3,4,5,1,0)
---------------------
Series3: (4,5,7,6,6,1,3)
Series4: (7,6)
UPDATE:
Your operator+(const TSeries &) should look somewhat like this:
TSeries operator+(const TSeries & other)
{
if (this->_size != other._size)
{
std::cout << "Size doesn't match!" << std::endl;
return TSeries(); //Return empty object
}
TSeries tmp(*this); //Create copy
for (int i = 0; i < tmp._size; i++)
{
tmp._tab[i] += other._tab[i];
}
return tmp;
}
And your operator()(int, int) like this:
TSeries operator()(int position1, int position2)
{
if (position1 < 0) position1 = 0;
else if (position1 >= _size) position1 = _size - 1;
if (position2 < position1) position2 = position1;
else if (position2 >= _size) position2 = _size - 1;
return TSeries(_tab + position1, position2 - position1);
}
Maybe you want to throw exceptions in the error cases?
Related
I'm trying to make a little game on c++ and I'm trying to overload the "=" operator. It is supposed to copy the game, and make the copy undependable. Looks like I'm doing something wrong with the memory or something like that, because when I try to print the copy it crashes. What am I doing wrong?
This is the overloading:
Game& Game::operator=(const Game& other){
std::vector<Pair> new_grid;
for(int i=0; i < other.grid_characters.size(); i++){
Character* character = other.grid_characters[i].character->clone();
std::shared_ptr<Character> character_shared_ptr(character);
Pair new_element( other.grid_characters[i].grid_point, character_shared_ptr);
new_grid.push_back(new_element);
}
this->height=other.height;
this->width = other.width;
this->grid_characters=new_grid;
return *this;
}
Overloading the "<<":
std::ostream& operator<<(std::ostream& os, const Game& game){
std::string grid_str_to_print;
for(int i=0; i < game.height; i++){
for(int j=0; j < game.width; j++){
GridPoint grid_point(i, j);
std::shared_ptr<Character> character = getCharacterAtPoint(game, grid_point);
if(character == nullptr){
grid_str_to_print += ' ';
} else {
grid_str_to_print += character->getCharSymbol();
}
}
}
char* output_grid = new char[grid_str_to_print.length() +1];
strcpy(output_grid, grid_str_to_print.c_str());
printGameBoard(os,output_grid, output_grid + grid_str_to_print.length() ,game.width);
delete[] output_grid;
return os;
}
And printing the board:
std::ostream &mtm::printGameBoard(std::ostream &os, const char *begin,
const char *end, unsigned int width) {
std::string delimiter = std::string(2 * width + 1, '*');
const char *temp = begin;
os << delimiter << std::endl;
while (temp != end) {
os << "|" << (*temp);
++temp;
if ((temp - begin) % width == 0)
os << "|" << std::endl;
}
os << delimiter;
return os;
}
This is the pair:
struct Pair {
GridPoint grid_point;
std::shared_ptr<Character> character;
Pair(GridPoint grid_point, std::shared_ptr<Character> character) :
grid_point(grid_point), character(character) {}
};
This is clone:
Character* Soldier::clone() const {
return new Soldier(*this);
}
This is the code in the main:
#include <iostream>
#include <cassert>
#include "Exceptions.h"
#include "Game.h"
using namespace mtm;
void example1() {
std::cout << "------example 2------" << std::endl;
Game g1(5,10);
g1.addCharacter(GridPoint(3,0), Game::makeCharacter(CharacterType::SOLDIER, Team::POWERLIFTERS, 20, 0, 3, 5));
Game g2 = g1;
std::cout << g1 << std::endl;
std::cout << g2 << std::endl;
std::cout << "Nice!" << std::endl;
}
int main() {
example1();
return 0;
}
There is no error returned and no messages whatsoever.
I am creating a dynamic array class that holds polynomials. The problem I am having right now is when I run my code, once it hits the return statement in main it begins to call the destructor and begins to free the memory from each instance starting with C. It deletes C fine, but when it gets to B I get a heap corruption error. I have tried walking through the code, but I cannot see where the corruption is happening. Can anyone help me? The exact error it gives me is "CRT detected that the application wrote to memory after end of heap buffer."
*Edit: I am more than happy to get peoples recommendations to help make my code better, but remember this is for a class and has specific rules. I cannot use anything from STL. I love any criticism you can give me.
///////////////////////////Header/////////////////////////////
class Poly
{
friend std::ostream& operator<<(std::ostream& output, const Poly& pNomial);
public:
Poly();
Poly(const int& coeff, const int& degree = 0);
Poly(const Poly& copy);
~Poly();
void setCoeff(const int& coeff, const int& degree);
bool isEmpty()const;
Poly& operator=(const Poly& pNomial);
private:
int* coeffs;
int highestDegree;
};
///////////////////////////CPP////////////////////////
#include "poly.h"
Poly::Poly()
{
highestDegree = 0;
coeffs = new int[highestDegree+1]();
}
Poly::Poly(const int & coeff, const int & degree)
{
if (degree >= 0)
{
highestDegree = degree;
coeffs = new int[highestDegree + 1]();
coeffs[degree] = coeff;
}
else
{
highestDegree = 0;
coeffs = new int[highestDegree + 1]();
}
}
Poly::Poly(const Poly& copy)
{
highestDegree = copy.highestDegree;
coeffs = new int[highestDegree + 1]();
for (int i = 0; i < copy.highestDegree + 1; i++)
{
coeffs[i] = copy.coeffs[i];
}
}
Poly::~Poly()
{
delete[] coeffs;
}
void Poly::setCoeff(const int& coeff, const int& degree)
{
if (degree > this->highestDegree)
{
Poly temp = *this;
delete[] this->coeffs;
this->highestDegree = degree;
this->coeffs = new int[highestDegree]();
for (int i = 0; i < temp.highestDegree + 1; i++)
{
this->coeffs[i] = temp.coeffs[i];
}
}
if (degree >= 0)
{
this->coeffs[degree] = coeff;
}
}
bool Poly::isEmpty()const
{
bool check = true;
for (int i = 0; i < highestDegree + 1 && check; i++)
{
if (coeffs[i] != 0)
{
check = false;
}
}
return check;
}
Poly & Poly::operator=(const Poly& pNomial)
{
if (this != &pNomial)
{
delete[] this->coeffs;
this->highestDegree = pNomial.highestDegree;
this->coeffs = new int[this->highestDegree + 1]();
for (int i = 0; i < pNomial.highestDegree + 1; i++)
{
this->coeffs[i] = pNomial.coeffs[i];
}
}
return *this;
}
std::ostream& operator<<(std::ostream& output, const Poly& poly)
{
if (!poly.isEmpty())
{
for (int i = poly.highestDegree; i >= 0; i--)
{
if (i == 1 && poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i] << "x";
}
else
{
output << " " << poly.coeffs[i] << "x";
}
}
else if (i == 0 && poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i];
}
else
{
output << " " << poly.coeffs[i];
}
}
else if (poly.coeffs[i] != 0)
{
if (poly.coeffs[i] >= 1)
{
output << " +" << poly.coeffs[i] << "x^" << i;
}
else
{
output << " " << poly.coeffs[i] << "x^" << i;
}
}
}
}
else
{
output << " 0";
}
return output;
}``
/////////////////////////////////Main/////////////////////////
#include "poly.h"
#include <iostream>
int main()
{
Poly A, B(5, 7), C(2);
B.setCoeff(2, 10);
B.setCoeff(1, 3);
B.setCoeff(5, 4);
std::cout << A << std::endl;
std::cout << B << std::endl;
std::cout << C << std::endl;
return 0;
}
I must say, that I agree with comments and you should seriously look into proper lifetime management of resources you use in the Poly class.
To answer the problem you're facing now have a look at the setCoeff() function.
this->coeffs = new int[highestDegree]();
should be changed to,
this->coeffs = new int[highestDegree + 1]();
With your current implementation, you allocate the array with highestDegree and in your for loop you access temp.coeffs[highestDegree] which is out of bounds access i.e. you loop till i < temp.highestDegree + 1.
I have written a program which was given to me as a homework assignment (it's a bit longer). The issue is that it compiles in CodeBlocks but it does not compile in Visual Studio 2017 it says - binary '=': no operator found which takes a right-hand operand of type 'CAutomobile' (or there is no acceptable conversion.
I would like to ask why is that because I could not myself find the error? I tried commenting the operator =function but still the error remained.
#include <iostream>
#include <algorithm>
#include <string>
#include <stdlib.h>
using namespace std;
class CVehicle {
string name;
int year;
public:
CVehicle() {
name = "Car";
year = 1990;
}
CVehicle(string n, int y) {
name = n;
year = y;
}
CVehicle(const CVehicle& vc) {
name = vc.name;
year = vc.year;
}
void setName(string n) {
name = n;
}
void setYear(int y) {
year = y;
}
string getName() {
return name;
}
int& getYear() {
return year;
}
virtual void Print(ostream& os) = 0;
};
class CAutomobile :public CVehicle {
double litres;
public:
CAutomobile() :CVehicle() {
litres = 7.2;
}
CAutomobile(string nm, int yr, double l) :CVehicle(nm, yr) {
litres = l;
}
void setLitres(double l) {
l = litres;
}
double& getLitres() {
return litres;
}
void Print(ostream& os) override {
os << getName() << endl;
os << getYear() << endl;
os << litres << endl;
}
friend bool operator< (CAutomobile a1, CAutomobile a2) {
if (a1.litres < a2.litres) {
return true;
}
return false;
}
CAutomobile operator= (CAutomobile& at) {
CAutomobile au;
au.getName() = at.getName();
au.getYear() = at.getYear();
au.getLitres() = at.getLitres();
return au;
}
CAutomobile operator+(CAutomobile aut) {
CAutomobile a;
a.getLitres() = getLitres() + aut.getLitres();
return a;
}
friend ostream& operator<< (ostream& o, CAutomobile a) {
o << a.getName() << endl;
o << a.getYear() << endl;
o << a.getLitres() << endl;
return o;
}
};
int main()
{
CAutomobile a[] = {
CAutomobile(),
CAutomobile("Wolkswagen",1970,80.5),
CAutomobile("Fiat",1979,21.9),
CAutomobile("Opel",1978,13.7)
};
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
cout << "Name" << ' ' << a[i].getName() << endl;
cout << "Year" << ' ' << a[i].getYear() << endl;
cout << "Litres" << ' ' << a[i].getLitres() << endl;
}
int range = 2016 - 1990 + 1;
for (int i = 0; i < sizeof(a) / sizeof(a[0]); i++) {
a[i].setLitres(rand() % 100 + 1);
a[i].setYear(rand() % range + 1996);
}
//сортираме масива по литри и извеждаме
//най малкия (първия) му елемент
for (int i = 0; i < sizeof(a-1); i++) {
for (int j = 0; j < sizeof(a-1); j++) {
if (a[j].getLitres() > a[j + 1].getLitres()) {
swap(a[j], a[j + 1]);
}
}
}
cout << a[0] << endl;
CAutomobile k = a[0] + a[3];
cout << k.getLitres() << endl;
}
CAutomobile::operator = is completely wrong. It takes a non-const reference and assignes its field to a new object. Instead it should take a const reference and modify current object.
CAutomobile & operator =(CAutomobile const & other)
{
assert(this != ::std::addressof(other)); // check for self-assignment
SetName(other.getName());
SetYear(other.getYear());
SetLitres(other.getLitres());
return *this;
}
This will bring up another problem: getters are not const-qualified, so they should be fixes as well:
string const & getName(void) const {
return name;
}
int const & getYear(void) const {
return year;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Could you please tell me where is the memory leak and explain why i did wrong? I can't find the problem and i don't find the answer on google.
#include <iostream>
using namespace std;
class Avion{
private:
static int autoincrementare;
char* nume;
int randuri;
int* locuri ;
const double pret = 100.00;
int nrPersonal;
bool servire;
public:
// setteri si getteri
int getRanduri(){
return this->randuri;
}
int getNrPersonal(){
return this->nrPersonal;
}
bool getServire(){
return this->servire;
}
char* getNume(){
return this->nume;
}
int* getLocuri(){
return this->locuri;
}
void setNume(char* nume){
if (nume != NULL) delete this->nume;
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
}
void setLocuri(int* locuri){
if (randuri != NULL) {
this->locuri = new int[this->randuri];
for (int i = 0; i < randuri; i++)
this->locuri[i] = locuri[i];
}
}
void setRanduri(int randuri){
this->randuri = randuri;
}
void setNrPersonal(int nrPersonal){
this->nrPersonal = nrPersonal;
}
void setServire(bool servire){
this->servire = servire;
}
//constructor fara parametrii
Avion() :pret(autoincrementare++){
this->randuri = 3;
this->servire = true;
this->nrPersonal = 10;
this->nume = new char[strlen("Luft") + 1];
strcpy(this->nume, "Luft");
this->locuri = new int[this->randuri];
for (int i = 0; i < this->randuri; i++){
this->locuri[0] = 30;
this->locuri[1] = 40;
this->locuri[2] = 50;
}
}
Avion(int randuri, bool servire, int nrPersonal, char* nume, int* locuri) :pret(autoincrementare++){
this->randuri = randuri;
this->servire = servire;
this->nrPersonal = nrPersonal;
//if (nume != NULL) delete this->nume;
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
// if (locuri != NULL)delete this->locuri;
this->locuri = new int(this->randuri);
for (int i = 0; i < randuri; i++)
{
this->locuri[i] = locuri[i];
}
}
friend ostream & operator<<(ostream & out, Avion & a){
out << "Avionul are " << a.randuri << " randuri" << endl;
out << "Avionul are deschis bufetul : " << a.servire << endl;
out << "Avionul are numarul de personal de: " << a.nrPersonal << endl;
out << "Avionul are numele : " << a.nume << endl;
out << " Avionul are: " << a.randuri << " randuri cu " << endl;
for (int i = 0; i < a.getRanduri(); i++){
cout << a.getLocuri()[i] << " locuri " << endl;
}
return out;
}
friend istream & operator >>(istream & in, Avion &a){
char aux[50];
cout << "Nume avion : "; in >> aux;
if (a.nume != NULL) delete[] a.nume;
a.nume = new char[strlen(aux) + 1];
strcpy(a.nume, aux);
return in;
}
Avion& operator=(const Avion& a){
this->randuri = a.randuri;
this->servire = a.servire;
this->nrPersonal = a.nrPersonal;
this->nume = new char[strlen(a.nume) + 1];
strcpy(this->nume, a.nume);
this->locuri = new int(this->randuri);
for (int i = 0; i < randuri; i++)
{
this->locuri[i] = a.locuri[i];
}
return *this;
}
~Avion(){
if (nume != NULL) delete[] this->nume;
if (locuri != NULL) delete[] this->locuri;
}
};
int Avion::autoincrementare = 1;
void main(){
Avion Luft;
cin >> Luft;
cout << Luft << endl;
cout << "================================"<<endl;
cout << "==========================" << endl;
int a[3]{10, 20, 30};
Avion BlueAir(3, true, 10, "Blue Air", a);
cout << BlueAir << endl;
/*
Avion G6;
G6 = Luft;
cout << G6 << endl;
cout << "==================";
cout << Luft << endl;
*/
}
In this code, the first line of the function is doing a delete instead of a delete[]. So that's at least one memory leak.
void setNume(char* nume){
if (nume != NULL) delete this->nume;
this->nume = new char[strlen(nume) + 1];
strcpy(this->nume, nume);
}
You have to match new with delete and new[] with delete[].
I'm probably missing something obvious here. This is my code (I'm just learning true C++, and I want to get some practice):
#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
char * value;
int len;
friend ostream & operator<<(ostream & os, String s);
public:
String();
String(const char * base);
~String();
String operator+(String s);
String operator*(int n);
int length();
};
String::String() {
this->value = new char[0];
this->len = 0;
}
String::String(const char * base) {
this->value = new char[this->len = strlen(base)];
strcpy(this->value, base);
}
String::~String() {
delete [] this->value;
}
int String::length() {
return this->len;
}
String String::operator+(String s) {
String n;
delete [] n.value;
cout << "Upon entering, I am: \"" << *this << "\"\n";
n.value = new char[this->len + s.len];
for(int i = 0; i < this->len; i++) {
n.value[i] = this->value[i];
}
for(int i = 0; i < s.len; i++) {
n.value[i + this->len] = s.value[i];
}
n.len = this->len + s.len;
cout << "String::operator+(" << *this << ", " << s << ") succeeded with new value = \"" << n << "\"\n";
return n;
}
String String::operator*(int n) {
String s;
delete [] s.value;
s.value = new char[this->len * n];
for(int i = 0; i < this->len * n; i++) {
s.value[i] = this->value[i % this->len];
}
cout << "String::operator* succeeded with new value = \"" << s << "\"\n";
return s;
}
ostream & operator<<(ostream & os, String s) {
return os << s.value;
}
int main() {
String s("Hello, world!");
cout << s << "\nLength = " << s.length() << "\n\n";
cout << (s + String("\n")) * 5;
return 0;
}
And the string initializes and displays correctly, but my output is really strange; it seems that upon entering the operator+, "Hello, world!" suddenly becomes "x%r"?
C:\Users\Ryan\Documents\My Dropbox\C++ Projects>strings
Hello, world!
Length = 13
Upon entering, I am: "x%r"
String::operator+(x%r,
) succeeded with new value = "x%r"
String::operator* succeeded with new value = "╚%r"
─
Try this:
ostream & operator<<(ostream & os, const String& s) {
return os << s.value;
}
otherwise your should define copy constructor for your String class.
You need to provide copy constructor and assignment operator.
There are a lot of problems with your code.
You are managing your own memory. You should avoid doing this if at all possible.
You are consitantly forgetting that strings have a null terminator. In order to accomodate the strin Hello, world! you need a char buffer that is 14 bytes, not 13.
You have a len member variable that does effectively the same thing as the strlen function, except for the inconsistent consideration of #1 above.
Your string class does not have a copy constructor, which results in wild pointers and eventually crashes.
Here is a refactoring of your code that pretty much works.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
using namespace std;
class String {
private:
char * value;
// size_t len;
friend ostream & operator<<(ostream & os, String s);
public:
String();
String(const char * base);
String(const String& rhs)
{
value = new char[strlen(rhs.value)+1];
strcpy(value,rhs.value);
}
~String();
String operator+(String s);
String operator*(int n);
size_t length();
};
String::String() {
this->value = new char[0];
}
String::String(const char * base) {
this->value = new char[strlen(base)+1];
strcpy(this->value, base);
}
String::~String() {
delete [] this->value;
}
size_t String::length() {
return strlen(value);
}
String String::operator+(String s) {
String n;
delete [] n.value;
cout << "Upon entering, I am: \"" << *this << "\"\n";
n.value = new char[strlen(value)+strlen(s.value)+1];
for(int i = 0; i < strlen(value); i++) {
n.value[i] = this->value[i];
}
for(int i = 0; i < strlen(s.value); i++) {
n.value[i + strlen(value)] = s.value[i];
}
n.value[strlen(value)+strlen(s.value)] = '\0';
cout << "String::operator+(" << *this << ", " << s << ") succeeded with new value = \"" << n << "\"\n";
return n;
}
String String::operator*(int n) {
String s;
delete [] s.value;
s.value = new char[(strlen(value)*n)+1];
for(int i = 0; i < strlen(value) * n; i++) {
s.value[i] = this->value[i % strlen(value)];
}
s.value[strlen(value)*n] = '\0';
cout << "String::operator* succeeded with new value = \"" << s << "\"\n";
return s;
}
ostream & operator<<(ostream & os, String s) {
return os << s.value;
}
int main() {
String s("Hello, world!");
cout << s << "\nLength = " << s.length() << "\n\n";
cout << (s + String("\n")) * 5;
return 0;
}