Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:47:30+00:00 2026-05-27T09:47:30+00:00

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

  • 0

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+’
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:47:30+00:00Added an answer on May 27, 2026 at 9:47 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want something like the following: foo.should_receive(:bar).with(an_instance_of(Bla))..... Is this possible? Edit: According to the
Is it possible to format price according to rules like this using DecimalFormat in
According to this article , it's possible to get multiline XML comments -- instead
Is this possible? function testObj(testArg) { //Do obj stuff here } testObj.prototype.addFn = function()
i want to play sound according to text of the label.is this possible to
I'm trying to use this fixed floating banner script in IE6 -> Demo According
I'm trying to create a wireframe vertex/fragment shader in Unity. It seems possible according
Is this possible to use Ajax.Beginform with update target inside of ajax form. like
Is this possible if I click on an anchor link to simulate as if
Is this possible? My code works on Firefox. I can also get webkit to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.