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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:06:27+00:00 2026-06-14T09:06:27+00:00

I’m learning how to use functors together with STL algorithms to calculate the dot

  • 0

I’m learning how to use functors together with STL algorithms to calculate the dot product of two vectors. Here are my codes:

template<size_t DIM>
double Vector<DIM>::operator*(const Vector<DIM>& rhs) const {
    double dotPro = 0;
    std::for_each(vec, vec + DIM, std::bind2nd(dot_product<double>(rhs.get()), dotPro));
    return dotPro;
}
/*vec is a double array and the data member of Vector class. I want to get the 
dot product of rhs and *this by using std::for_each(). 
rhs.get()returns a const double* which is the start address of rhs's vec*/

/*The codes below define the functor.  dotPro is passed as a reference so as to 
it could be save the last result.*/  
template<typename T>
struct dot_product: public std::binary_function<T, T, void> {
    const T* arg;
    sum(const T* dbl) : arg(dbl){};
     void operator() (const T dbl, T& dotPro) {
        dotPro += *arg++ * dbl;
    }
};

Sorry I forget my quesion…
The question is my code doesn’t compile. Here is the compiling error:

error: no match for call to '(const dot_product<double>) (const double&, const double&)'|
note: candidates are: void dot_product<T>::operator()(T, T&) [with T = double]|

And here is a error occurs in binders.h

c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\backward\binders.h|147|error: return-statement with a value, in function returning 'void'|
  • 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-06-14T09:06:29+00:00Added an answer on June 14, 2026 at 9:06 am

    Just use std::inner_product:

    live demo

    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <ostream>
    #include <numeric>
    #include <cstdlib>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<int> v1,v2;
        generate_n( back_inserter(v1), 256, rand );
        generate_n( back_inserter(v2), v1.size(), rand );
        cout << inner_product( v1.begin(), v1.end(), v2.begin(), 0 ) << endl;
    }
    

    Yes, that’s a good solution. But I don’t want to use any methods provided in numeric.h.

    Just define your own dot_product:

    live demo

    template<typename InputIterator1,typename InputIterator2,typename ValueType>
    ValueType dot_product( InputIterator1 first, InputIterator1 last, InputIterator2 another, ValueType init)
    {
        while(first!=last)
        {
            init += (*first) * (*another) ;
            ++first;
            ++another;
        }
        return init;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

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.