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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:30:20+00:00 2026-05-25T20:30:20+00:00

Hi I am have written the following function for adding 2 vectors vector<double> add(vector<double>&

  • 0

Hi I am have written the following function for adding 2 vectors

vector<double> add(vector<double>& v1, vector<double>& v2 )

{  /*Do quick size check on vectors before proceeding*/

  vector<double> result(v1.size());
  for (unsigned int i = 0; i < result.size(); ++i)
  {
    result[i]=v1[i]+v2[i];
  }

  return result;

}

Now I tried to add 3 vectors a,b,c all of same size in the following two ways

vector <double> sum1, sum2;

sum1=add(b,c);

sum2=add(a,sum1); 

which worked

and

vector <double> sum;
sum=add(a,add(b,c));

which gave me the following compiler error

g++ -Wall simple.cpp 
simple.cpp: In function ‘int main(int, char**)’:
simple.cpp:45: error: invalid initialization of non-const reference of type ‘std::vector<double, std::allocator<double> >&’ from a temporary of type ‘std::vector<double, std::allocator<double> >’
simple.cpp:16: error: in passing argument 2 of ‘std::vector<double, std::allocator<double> > add(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)’

Why did the second method not work?

  • 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-25T20:30:21+00:00Added an answer on May 25, 2026 at 8:30 pm
    vector<double> add(vector<double> v1, vector<double> v2 )
    

    From the error message, I can say with 100% certaintly that this is not the function signature in your original code.

    In your original code, you must be using this:

    vector<double> add(vector<double> & v1, vector<double>  & v2 )
    

    If so, then make it as:

    vector<double> add(const vector<double> & v1, const vector<double>  & v2)
    

    Now it should work just fine.

    Explanation:

    The return value of add() is a temporary object, and a temporary object cannot be bound to non-const reference. That is why the compiler was giving error. It will give same error if you write this:

    vector<double> & v = add(a,b); //error 
    

    However, if a temporary object can be bound to const reference. That is why I suggested you to make the parameter const reference. That means, you can write this:

    const vector<double> & v = add(a,b); //ok
    

    Also, you can make the parameter non-const non-reference as well, that is, pass the arguments by value. But I would not recommend that, as it involves unnecessary copies of the vectors.

    The best solution is this:

    vector<double> add(const vector<double> & v1, const vector<double>  & v2)
    

    Getting familiar with operator+

    You could also overload operator+ for vector, as:

    vector<double> operator + (const vector<double>& v1, const vector<double>& v2 )
    {  
      /*Do quick size check on vectors before proceeding*/
      vector<double> result(v1.size());
      for (unsigned int i = 0; i < result.size(); ++i)
      {
        result[i]=v1[i]+v2[i];
      }
      return result;
    }
    

    If you overload this, then you can write this cool code:

    vector<double> c = a + b;
    vector<double> d = a + b + c;
    vector<double> e = a + b + c + d;
    //so on
    

    Isn’t it fun?

    Getting familiar with Standard algorithm..

    As @Gene commented, you could use std::transform in the function as:

    vector<double> operator+(const vector<double>& v1, const vector<double>& v2 )
    {  
       vector<double> result;
       std::transform(v1.begin(),v1.end(),v2.begin(), 
                                std::back_inserter(result), std::plus<double>);
       return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: std::vector<double>residuals; std::cout << Print_res(std::cout); std::ostream& Print_res(std::ostream& os) const {
I have written the following template function for summing the contents of a std::vector
I have written following function public void TestSB() { string str = The quick
I have written the following function to calculate a check digit in R. verhoeffCheck
I have written the following code: $(document).ready(function () { $(#rade_img_map_1335199662212).hover(function () { $(li#rs1).addClass(active); //Add
I have written the following function but it's isn't returning anything when I run
I have written the following algorithm in order to evaluate a function in MatLab
I have written following function which checks whether start_date field is not empty and
I have written the following function: it takes in a variable input_name ; The
I have written the following function meant to update a student in my database:

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.