1

Is this possible? According to what I'm trying to accomplish, it appears not so.

The function

static std::string str_repeat(std::string * str, int num_times) {

    std::string * str_rep = new std::string;

    for (int n = 1; n <= num_times; n++) {
        str_rep = str_rep + str;
    }

    std::string ret = *str_rep; //error

    delete str;
    delete str_rep;

    return ret;
}

Update

Sorry, I didn't post the error in the first place because I thought it was a universal C++ issue that I was doing wrong. Here it is.

error: invalid operands of types ‘std::string* {aka std::basic_string<char>*}’ and ‘std::string* {aka std::basic_string<char>*}’ to binary ‘operator+’
trincot
  • 317,000
  • 35
  • 244
  • 286
zeboidlund
  • 9,731
  • 31
  • 118
  • 180

4 Answers4

7

First of all, if you ever say new std::string then you're probably doing something wrong. This code should not have any pointers in it (and str_rep = str_rep + str is pointer arithmetic in your code, not append, that's why dereferencing the result fails).

std::string str_repeat(const std::string& str, int num_times) {
    std::string ret;
    for (int n = 0; n < num_times; ++n) {
        ret += str;
    }
    return ret;
}
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
1

I'm guessing here because that's what you asked for. When you decide to tell the world what exactly "error" means, I might need to revise the answer.

I'm guessing that you have a run-time error because *str_rep is garbage.

It is garbage because of this portion:

for (int n = 1; n <= num_times; n++) {
    str_rep = str_rep + str;
}

Both str_rep and str are pointers, and you're adding one to the other, but what are they pointing to? If you want to append the strings, do:

for (int n = 1; n <= num_times; n++) {
    *str_rep = *str_rep + *str;
}

Or just don't use pointers at all, don't see any benefit in doing so.

littleadv
  • 20,100
  • 2
  • 36
  • 50
1

operator + on a std::string * means pointer manipulation, not string concatenation. You don't need to jump through any of those hoops though; the std::string will internally allocate a buffer large enough for its contents. Change your function to this:

static std::string str_repeat(const std::string& str, int num_times) {
    std::string result("");

    for (int n = 1; n <= num_times; n++) {
        result += str;
    }

    return result;
}

And call it passing in the string proper, rather than the address of the string:

std::string myString(...);
std::string str = str_repeat(myString, 10);
std::string str2 = str_repeat("foobar", 100);

I feel like there's already a standard library function for exactly this purpose, though I can't think of it offhand.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
1

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;
}
Jon Rea
  • 9,337
  • 4
  • 32
  • 35