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

  • Home
  • SEARCH
  • 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 6919345
In Process

The Archive Base Latest Questions

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

Currently I’m trying to rewrite the += operator for a class I wrote called

  • 0

Currently I’m trying to rewrite the += operator for a class I wrote called mystring:

MyString& operator+=(MyString& s1, const MyString& s2)
{

    int newStringLength = s1.length + s2.length;
    char* newStorage = new char[newStringLength +  1];

    strcpy(newStorage, s1.data);

    strcpy(newStorage + s1.length, s2.data);
    delete[] s1.data;

    s1.length = newStringLength;
    s1.data = newStorage;

    return s1;

}

MyString operator+(const MyString& s1, const MyString& s2)
{

    MyString temp;
    delete[] temp.data;

    temp.length = s1.length;
    temp.data = new char[temp.length+1];

    strcpy(temp.data, s1.data);
    temp+=s2;

    return temp;

}

Where length is the length of the string and data is a string stored in char * format.

The program works fine when I try to do something like:

MyString test1 = "hi";
MyString test2 = "to"; 

test1 += test2;

But does not work when I try something like:

   MyString test;
    MyString test1 = "hi";
    MyString test2 = "to"; 

    test += test2 + test1
          += "you";

Basically when I start mixing += and + in an alternating way it doesn’t work. Here is the error at compilation:

testoutput.cpp:26: error: no match for ‘operator+=’ in ‘operator+(const MyString&, const MyString&)(((const MyString&)((const MyString*)(& test1)))) += "you"’
mystring.h:45: note: candidates are: MyString& operator+=(MyString&, const MyString&)

Does anyone have any idea how I can change my code in order to achieve this functionality?

  • 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:58:42+00:00Added an answer on May 27, 2026 at 9:58 am

    It doesn’t make sense to mix + and += in such a way. I’m not really sure what your intended behavior is, but if you want the nested += to apply to test1, you’ll have to use parenthesis:

    test += test2 + (test1 += "you");
    

    This is not a problem with your assignment operator, but with operator precedence in the language. If you would replace MyString with int you would run into the same problems.

    The precedence and associativity of the + and += operators causes the expression without parenthesis to be interpreted like this:

    test += ((test2 + test1) += "you");
    

    This tries to assign to test2 + test1, but that’s not possible (you can only assign to variables). This operator precedence cannot be changed and without parenthesis the expression will always be interpreted this way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have: <html> <div class=yes1><span><img src=img></span></div> </html> <script> var x = ='yes1' var
Currently I am adding object by creating it like: type TRecord = class private
Currently, my MVC 3 app has a dependency on a static class that is
Currently, I use an abstract factory to allow the specification of a custom class
Currently I'm learning C++ and I've been trying to create a simple image processing
Currently I’m trying to get the hang of a block copy with my current
Currently, I'm using && and || instead of AND and OR because that's how
Currently I'm trying to do what I thought would be a simple task: Draw
Currently, I don't really have a good method of debugging JavaScript in Internet Explorer and
Currently my workflow with Emacs when I am coding in C or C++ involves

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.