Functions with same name overload in the same class

Hello,

I’m struggling with function overloading. I would like to have two different functions that have the same name but different parameters. I am pretty unfamiliar with C++, and I thought by having different types of parameters, C++ would be able to distinguish two functions. Below is a minimal example of the problem I am having, and basically I would like to change otherfunction(TH1 *hist_beta, Double_t tau) to function(TH1 *hist_beta, Double_t tau).

class A : public TObject {
    public:    
      TVectorD function(TMatrixD *beta, Double_t tau);                    
      TVectorD otherfunction(TH1 *hist_beta, Double_t tau);  
}

Here, otherfunction merely changes the type TH1* to TMatrixD* and just calls the function as below:

TVectorD A::otherfunction(TH1 *hist_beta, Double_t tau)
{
  TMatrixD *beta;
  beta = new TMatrixD(some number, 1);
  for (Int_t i = 0; i <some number; i++) {
    (*beta) (i, 0) = hist_beta->GetBinContent(some array);
  }

  TVectorD result = function(beta, tau); 
  return result;
}

If I compile as of now and employ otherfunction in some other script, it works fine. However, when I change the name otherfunction to function, it still compiles fine, but when I call function(TH1D* x, 1.0);, I get the following error message:
cannot initialize a parameter of type 'TMatrixD *' (aka 'TMatrixT<double> *') with an lvalue of type 'TH1D *.

So I think ROOT is able to recognize the first function(TMatrixD *beta, Double_t tau), but not the second function(TH1 *hist_beta, Double_t tau). Could anyone please help me with this issue? Thank you so much in advance!

Hi lylejkim,
function overloading certainly works as you expect it to work (and ROOT certainly supports it :slight_smile:).

Can you post the smallest snippet of code that still reproduces the issue and is compilable/interpretable (i.e. can you post a minimal reproducer)?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.