简体   繁体   中英

Why operator() Function cannot be overloaded?

I need to define a new function using the same parameters from function which is defined already. Could it be even possible? I tried doing that but getting the error

                          error: cannot be overloaded

Here is code listing:

#ifndef CORRELATOR_MEAN_SQUARE_DISPLACEMENT_HPP
#define CORRELATOR_MEAN_SQUARE_DISPLACEMENT_HPP

#include<halmd/numeric/blas/fixed_vector.hpp>
#include<typeinfo>
#include <boost/multi_array.hpp>
#include <memory>

namespace correlator {

class mean_square_displacement
{
public: 
    typedef std::shared_ptr<boost::multi_array<float, 2>> sample_type;
    typedef double result_type;  // fixed_vector<double, 3>
    
    result_type operator() (sample_type const& first, sample_type const& second) const
    { 
        double msd = 0;
        size_t N = first->size(); 
       
        for (unsigned int i = 0; i < N; ++i) {
            for (unsigned int j = 0; j < 3; ++j) {
                double dr = (*first)[i][j] - (*second)[i][j];
                msd += dr * dr;
                if (msd > 0)
                    std::cout << "msd::" <<  msd << std::endl;
            } 
        }
   
        return msd/N;
    } // msd operator ends here
    
    result_type operator() (sample_type const& first, sample_type const& second) const
    {
        return (std::cout << (*first)[0][0]<< "The first element :." << std::endl);
        
    }

};

} // namespace correlator

#endif /* ! CORRELATOR_MEAN_SQUARE_DISPLACEMENT_HPP */

What I'm trying to do is use parameters(first and second) from first operator function and use them for different calculations in the second function? What am I doing wrong here. I intentially not posting all the files. IF someone wants I can share with them

It isn't possible to overload the same function name with the same signature (parameters and const/non-const). You should probably declare a new named function rather than overload the () operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM