Lots of errors when running my root program


ROOT Version: 6.18/04
Platform: VSCode
Compiler: VSCode
System: Mac OSX


I am a beginner to ROOT and after downloading it on my Mac and running the following macro:

#include <iostream>
#include <cmath>
using namespace std;

void f1(){

    c1 = new TCanvas("c1", "My RoOt Plots", 1500, 500);
    c1 -> Divide(3,1);

    c1 -> cd(1);
    f = new TF1("f", "sin(x)", 0, 5);
    f -> SetLineColor(kBlue + 3);
    f -> SetTitle("MyFunction; xval; yval");
    f -> Draw();

    c1 -> cd(2);
    g = new TF1("g", "cos(x)", 0, 5);
    g -> SetLineColor(kMagenta);
    g -> SetTitle("2ndFunction; xval2; yval");
    g -> Draw();

    c1 -> cd(3);
    h = new TF2("h", "x^2 + y^2", 0, 5, 0, 5);
    h -> SetLineColor(kPink);
    h -> SetTitle("OtherFunction; x; y");
    h -> Draw("surf1");

    

    cout << "\n";
}

When I run the command .x f1.C in the Mac terminal, it gives the following errors:

/Users/myname/Documents/root2/macros/f1.C:7:5: error: use of undeclared identifier 'c1’

c1 = new TCanvas(“c1”, “My RoOt Plots”, 1500, 500);

^

/Users/myname/Documents/root2/macros/f1.C:8:5: error: use of undeclared identifier 'c1’

c1 -> Divide(3,1);

^

/Users/myname/Documents/root2/macros/f1.C:10:5: error: use of undeclared identifier 'c1’

c1 -> cd(1);

^

Furthermore, the top two lines are underlined in red because I need to update my include path and that I cannot open the source file for either. How do I fix this?

Also, what lines should I have out in my .bash profile to make this work?

Hi, asp211

In your root macro, c1, f, g and h is constructor.

c1= new TCanvas(~);
f=new TF1(~);

This writhing is insufficient. It isn’t defined.
You should define as follows.

TCanvas *c1=new TCanvas(~);
TF1 *f=new TF1(~);

Thia is correct writing.

I modified your code as fallows.

using namespace std;
void f1(){
  TCanvas *c1 = new TCanvas("c1", "My RoOt Plots", 1500, 500);
  c1 -> Divide(3,1);

  c1 -> cd(1);
  TF1 *f = new TF1("f", "sin(x)", 0, 5);
  f -> SetLineColor(kBlue + 3);
  f -> SetTitle("MyFunction; xval; yval");
  f -> Draw();

  c1 -> cd(2);
  TF1 *g = new TF1("g", "cos(x)", 0, 5);
  g -> SetLineColor(kMagenta);
  g -> SetTitle("2ndFunction; xval2; yval");
  g -> Draw();

  c1 -> cd(3);
  TF2 *h = new TF2("h", "x^2 + y^2", 0, 5, 0, 5);
  h -> SetLineColor(kPink);
  h -> SetTitle("OtherFunction; x; y");
  h -> Draw("surf1");

  c1->cd(0);

  cout << "\n";
}

Maybe, you can run by typing “root f1.cc” in command line.