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 6898437
In Process

The Archive Base Latest Questions

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

I want to overload the operator + in a class which has a vector

  • 0

I want to overload the operator + in a class which has a vector as member variable, so as to carry out the merging of vectors of two different objects. In other words I would like a new object to be created which has as vector both the elements of the vectors of the two initial vectors in a row. I try the following code and I receive an error in operator+ regarding std::copy. What is the problem?

class TestVector
{
    std::vector<int> myVector;

public:
    TestVector(){};
    TestVector(std::vector<int>);
    std::vector<int> getVector();
    TestVector operator +(TestVector);

};

std::vector<int> TestVector::getVector()
{
   return myVector;
}

TestVector TestVector::operator+(TestVector param)
{

    std::vector<int> tempVector;
    std::vector<int> paramVector = param.getVector();

    std::copy(paramVector.begin(), paramVector.end(), tempVector);
    std::copy(myVector.begin(), myVector.end(), tempVector.end());

    TestVector TestVector1(tempVector);

    return TestVector1;

}

Moreover, is the second copy statement valid to merge two vectors in general?

Update: I got an error at execution time saying that iterators are inompatible at this statement. What is wrong?
tempVector.insert(tempVector.end(),param.getVector().begin(), param.getVector().end());

  • 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-27T07:17:47+00:00Added an answer on May 27, 2026 at 7:17 am

    The third argument to std::copy must be an iterator, not a container. You can use std::back_inserter thus:

    std::copy(paramVector.begin(), paramVector.end(), std::back_inserter(tempVector));
    

    The second copy will compile, since std::end() returns an iterator, but it will break because the iterator simply points to the end of the vector. It won’t grow the vector when std::copy tries to assign to it; it will simply invoke undefined behaviour. Again, std::back_inserter solves the problem.

    In any event you don’t need std::copy at all, since std::vector provides the necessary semantics directly:

    TestVector TestVector::operator+(const TestVector& v)
    {
        std::vector<int> t(myVector);
        t.insert(t.end(), v.myVector.begin(), v.myVector.end());
        return TestVector(t);
    }
    

    And remember to use const &… to avoid so much copying. For instance, the second constructor copies the input vector unnecessarily, and getVector() copies the internal vector unnecessarily.

    Even the above solution copies the temporary vector at least once, which can be avoided with a purpose-built constructor:

    class TestVector
    {
        std::vector<int> myVector;
    
    public:
        TestVector() { }
        TestVector(const std::vector<int>& v) : myVector(v) { }
        const std::vector<int>& getVector() const { return myVector; }
        TestVector operator+(const TestVector& v) const {
            return TestVector(op_add(), *this, v);
        }
    
    private:
        struct op_add { };
        TestVector(op_add, const TestVector& a, const TestVector& b) : myVector(a) {
            myVector.insert(myVector.end(), b.myVector.begin(), b.myVector.end());
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple class for which I want to overload operator as below
I have a Card class and I want to overload the > operator to
I have two or more variables of class object in c# which has integer
I have a smart pointer class and I want to overload operator-> ; it's
I'm writing a collection class. I want to overload the square brackets operator ([])
I want to overload new operator in a template class. But something wrong happends.
I want to overload the >> operator in c++ so that my class of
I want to overload operator<< for my class. Should I add this overloaded definition
I want to overload failUnlessEqual in unittest.TestCase so I created a new TestCase class:
I've got two instances of a PHP5 Class (say ClassA), and I want 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.