Error: function definition is not allowed here void function() {

I’m trying to learn how to fit a 2D histogram. I copied everything from here & created my own macro: https://root.cern.ch/root/html/tutorials/fit/fit2.C.html
and I enclosed it all in { }, otherwise I got the message “warning: cannot find function ‘test2()’; falling back to .L” ( I named my macro test2.C)
After enclosing the whole code in { }, I got this error message:

/Applications/root/macros/test2.C:24:44: error: function definition is not allowed here
Double_t fun2(Double_t *x, Double_t *par) {
^
/Applications/root/macros/test2.C:32:13: error: function definition is not allowed here
void fit2() {

Why could this be? Also, how come there’s no error message about the 3rd function? (Double_t g2)

Hi,
the code in your macro must be valid C++, and must contain a function named test2 that will be executed when you run the macro with root. So the content of test2.C must be something like this:

void test2() {
 // your code goes here
}

//void optionalHelperFunction() {
//
//}

Thanks for you reply, do you mean that the whole thing should be enclosed within void test2() { here } ? Because unfortunately that still gives the same error messages, for all 3 functions within the code.

The content of the file must be valid c++. you cannot define functions inside functions in c++ :sweat_smile:

The way macros work is that, if your filename is test2.C, running your macro in root is (roughly) equivalent to executing test2().

ah, I see, unfortunately I don’t know much C++, but I’ll need to use ROOT for a summer internship. I renamed fit2 to test2 and now it works. Thank you for your help :slight_smile: Sorry for being a bit silly…

1 Like

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