0

Exercise 12-5 in Accelerated C++.

My operator+= works:

void Str2::operator+=(const Str2& a)
{
	iterator new_data = alloc.allocate((avail - data) + a.size());
	iterator new_avail = std::uninitialized_copy(data, avail, new_data);
	new_avail = std::uninitialized_copy(a.data, a.avail, new_avail);

	uncreate();

	data = new_data;
	limit = avail = new_avail;
}

However i cant get the operator+ to work:(

Str2 operator+(const Str2& a, const Str2& b)
{
	Str2 r = a;
	r += b; 
	return r;
}

Error: Debug Assertion Failed!
Expression: _BLOC_TYPE_IS_VALID(pHead->nBlockUse)

Its probably something obvious.
Any help is appreciated!!

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Andreas5
Featured Replies
  • 1

    > My operator+= works > However i cant get the operator+ to work The problem seems to be in your copy constructor; operator+ needs to make a copy. First, verify that the copy constructor [icode]Str2::Str2( const Str2& )[/icode] is working correctly. Read More

  • How can your += operator work?! It doesn't return anything. The declaration should be: [CODE=c++] Str2 & Str2::operator+=(const Str2& a); [/CODE] and in the implementation you should return *this. Same for the + operator. You can read about operator overlading [URL="http://www.cs.caltech.edu/courses/cs11/material/cpp/donnie/cpp-ops.html"]here[/URL]. Read More

1

> My operator+= works
> However i cant get the operator+ to work

The problem seems to be in your copy constructor; operator+ needs to make a copy.

First, verify that the copy constructor Str2::Str2( const Str2& ) is working correctly.

1

How can your += operator work?! It doesn't return anything. The declaration should be:

Str2 & Str2::operator+=(const Str2& a);

and in the implementation you should return *this. Same for the + operator.
You can read about operator overlading here.

0

How can your += operator work?! It doesn't return anything.

I can only use += after a Str2. I do the work on that Str2 object so i dont have to return anything. I dont know if its better or worse, but it works :P

The problem seems to be in your copy constructor; operator+ needs to make a copy.

I had forgot to make a copy constructor, + works fine now, thanks!

Edited by Andreas5: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.