Undeclared Identifier when running macros


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Having a strange issue when trying to follow some examples in both documentation and video tutorials. I keep getting an “undeclared identifier” when trying to get root to run a .C file that has been written in visual studio.

Any help would be much appreciated.

#include <iostream>

std::cout << ...

Thanks, now I’m getting this output

Could you attach the macro?

// Example drawing the interference pattern of light
// falling on a grid with n slits and ratio r of slit
// width over distance between slits.

#include <iostream>

auto pi = TMath::Pi();

// function code in C
double single(double* x, double* par) {
	return pow(sin(pi * par[0] * x[0]) / (pi * par[0] * x[0]), 2);
}

double nslit0(double* x, double* par) {
	return pow(sin(pi * par[1] * x[0]) / sin(pi * x[0]), 2);
}

double nslit(double* x, double* par) {
	return single(x, par) * nslit0(x, par);
}

//this is the main program
void Slits() {
	float r, ns;

	//request user input
	std::cout << "slit width / g ";
	scanf("%f", &r);
	std::cout << "# of slits? ";
	scanf("%f", &ns);
	std::cout << "interference pattern for " << ns
		 << " slits, width/distance: " << r << std::endl;

	// define function and set options
	TF1* Fnslit = new TF1("Fnslit", nslit, -5.001, 5., 2);
	Fnslit->SetNpx(500);

	// set parameters, as read in above
	Fnslit->SetParameter(0, r);
	Fnslit->SetParameter(1, ns);

	// draw the interference pattern for a grid with n slits
	Fnslit->Draw();
}

This was just taken from the ROOTPrimer documentation

Try to add:
#include <cmath>

It works fine with ROOT master:

C:\Users\bellenot\rootdev>root -l
root [0] .x Slits.C
slit width / g 0.2
# of slits? 2
interference pattern for 2 slits, width/distance: 0.2
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1]

So it’s maybe an issue your version of ROOT (since you didn’t provide which version you are using…)

Sorry, I’m using version 6.18.04 on Windows 10. What is ROOT master exactly?

It’s the current, unreleased version of ROOT in Github. I will try with 6.18.04 to see if it’s an issue which has been fixed in the meanwhile

Oh I see. Thanks very much.

So ROOT v6.18.04 works for me as well… Do you have any warning message when starting ROOT? Which version of Visual Studio do you have?
You can see the version when opening a x86 Native Tools Command Prompt for VS 2019:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.6
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

I just noticed that I do get some errors when I open root

I’m using visual studio v16.5.0

It works as well for me with Visual Studio v16.5.5 and ROOT v6.18.04
And the weird part is that I don’t have any error message when starting ROOT:

**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.5.5
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86'

C:\Users\bellenot>cd rootdev

C:\Users\bellenot\rootdev>..\Downloads\root\bin\thisroot.bat

C:\Users\bellenot\rootdev>root
   ------------------------------------------------------------
  | Welcome to ROOT 6.18/04                  https://root.cern |
  |                               (c) 1995-2019, The ROOT Team |
  | Built for win32 on Sep 11 2019, 15:38:23                   |
  | From tags/v6-18-04@v6-18-04                                |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

root [0] .x Slits.C
slit width / g ? 0.2
# of slits? 2
interference pattern for 2 slits, width/distance: 0.2
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1]

Really strange. Do you think I could just try uninstalling and reinstalling?

I don’t think so, I’m trying to understand. Let me check something first…

I’m suspecting the Windows 10 SDK, can you check that you have the version 10.0.18362.0?

FYI, it works on a Computer updated this morning (Windows version 10.0.17763.1217 and Visual Studio v16.5.5) even from a regular Command Prompt. (with the Windows 10 SDK version 10.0.18362.0 and 10.0.17763.0 installed):

Microsoft Windows [Version 10.0.17763.1217]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\bellenot>cd rootdev

C:\Users\bellenot\rootdev>..\Downloads\root\bin\thisroot.bat

C:\Users\bellenot\rootdev>root -l
root [0] .x Slits.C
slit width / g ? 0.2
# of slits? 2
interference pattern for 2 slits, width/distance: 0.2
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
root [1]

Sorry, how do I check my sdk version? Can’t seem to find it. I’ve got an update ready to install for visual studio too, could that be the issue?

In the Visual Studio Installer, select Modify, and you can see the Windows SDKs in the Installation details