简体   繁体   English

为什么operator() Function不能重载?

[英]Why operator() Function cannot be overloaded?

I need to define a new function using the same parameters from function which is defined already.我需要使用来自已经定义的 function 的相同参数来定义一个新的 function。 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?我想要做的是使用来自第一个运算符function 的参数(第一个和第二个)并将它们用于第二个 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).不可能用相同的签名(参数和 const/non-const)重载相同的 function 名称。 You should probably declare a new named function rather than overload the () operator.您可能应该声明一个名为 function 的新名称,而不是重载()运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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