You are trying to add two pointers together, which is why it wont compile. Don't forget that a pointer is a memory address, the +operator wont be called in your example - you would have to dereference the pointer, but I wouldn't recommend that pattern in this case. I'd suggest you read up a little more about pointers and references :-)
Be very careful about when you delete memory. It's bad practice to delete memory away from the context in which it was allocated - that's a recipe for bugs. Also, if you allocated on the stack before calling 'delete str', your application would likely crash.
For string manipulation I would really recommend passing by const reference. Thus will will deal with memory allocation for you, as you can pass std::string by value, and it will internally perform memory allocation as needed...
Another couple of points.
In C, languages, we usually count from '0', so I would change your for loop
In a proper application, I would have some debug assert on your input parameters: i.e. you should assert that num_times is '>0'
The following code compiles and executes with result "barbarbar"...
Cheers and good luck,
Jon
#include <string>
#include <iostream>
using namespace std;
static string str_repeat(const string& str, int count)
{
string returnData;
for (int n = 0; n < count; n++)
{
returnData.append(str);
}
return returnData;
}
int main(int argc, char* argv[])
{
string foo = "bar";
string duplicated = str_repeat(foo, 3);
cout << duplicated;
return 0;
}