Home | News | Documentation | Download

Error: no matching constructor for initialization

Hello everyone,

I am trying to read data from a txt file and I keep bumping into this:

***error:** **no matching constructor for initialization of 'std::fstream' (aka 'basic_fstream<char>')**

Here is the code:

fstream file;
    file.open("data2.txt", ios::in);

    while(1)
    {
        double x, y;
        file >> x >> y;
        gr->SetPoint(gr->GetN(), x, y);
        if(file.eof()) break;
    }

    file.close();

    TCanvas *c1 = new TCanvas();
    gr->Draw("ALP");

Any help would be appreciated.
Thank you.


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22
Platform: MacOS
Compiler: C


Hi @RPaun,
and welcome to the ROOT forum.
This does not seem a ROOT error, but a plain C++ problem.
Which line triggers the error? Can you remove all other lines?
If it’s fstream file;, as it seems from the error message: how are you executing that code? It works e.g. on lxplus:

[eguiraud@lxplus7117 ~]$ source /cvmfs/sft.cern.ch/lcg/views/LCG_98python3/x86_64-centos7-gcc10-opt/setup.sh                                                                
[eguiraud@lxplus7117 ~]$ root                                                                                                                                               
fstream file;   ------------------------------------------------------------------                                                                                          
  | Welcome to ROOT 6.22/00                        https://root.cern |                                                                                                      
  | (c) 1995-2020, The ROOT Team; conception: R. Brun, F. Rademakers |                                                                                                      
  | Built for linuxx8664gcc on Jun 14 2020, 15:54:05                 |                                                                                                      
  | From tags/v6-22-00@v6-22-00                                      |                                                                                                      
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q'       |                                                                                                      
   ------------------------------------------------------------------                                                                                                       
                                                                                                                                                                            
root [0] fstream file;                                                                                                                                                      
root [1] .q  

Cheers,
Enrico

Hello,

I wrote the code in a .C file. Then I ran the code in ROOT. I get the error in root. The problem was/is with fstream.

Hi,
the code you pasted is not a complete ROOT macro. This is what the corresponding macro should look like:

void macro() {
  fstream file;
  file.open("data2.txt", ios::in);

  TGraph *gr = new TGraph();

  while (1) {
    double x, y;
    file >> x >> y;
    gr->SetPoint(gr->GetN(), x, y);
    if (file.eof())
      break;
  }

  file.close();

  TCanvas *c1 = new TCanvas();
  gr->Draw("ALP");
}

I can execute that code correctly in ROOT v6.22 e.g. with root -l macro.C, or with:

$ root -l
root [0] .x repro.C                                                                  

What am I doing differently?
Cheers,
Enrico

Hello Enrico,

Here is the code:

void tutorial_4()
{
    fstream file;
    file.open("data2.txt", ios::in);
    TGraph *gr = new TGraph();

    while(1)
    {
        double x, y;
        file >> x >> y;
        gr->SetPoint(gr->GetN(), x, y);
        if(file.eof()) break;
    }

    file.close();

    TCanvas *c1 = new TCanvas();

    gr->SetMarkerStyle(kFullCircle);
    gr->SetLineWidth(2);
    gr->SetLineColor(kRed);
    gr->SetTitle("Graph");
    gr->GetXaxis()->SetTitle("X Values");
    gr->GetYaxis()->SetTitle("Y Values");
    gr->Draw("ALP");
}

Here are the errors:

root [3] .x tutorial_4.C

In file included from input_line_11:1:

/Users/rpaun/Desktop/Workspace/Examples/root-tutorials-examples/physics-matters-tutorial/tutorial_4.C:14:10: error: call to member function 'open' is ambiguous

file.open("data2.txt", ios::in);

~~~~~^~~~

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/fstream:1607:10: note: candidate function

void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);

^

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/fstream:1611:10: note: candidate function

void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)

^

In file included from input_line_11:1:

/Users/rpaun/Desktop/Workspace/Examples/root-tutorials-examples/physics-matters-tutorial/tutorial_4.C:24:10: error: no member named 'close' in 'std::__1::basic_fstream<char, std::__1::char_traits<char> >'

file.close();

~~~~ ^

root [4] .x tutorial_4.C

In file included from input_line_12:1:

/Users/rpaun/Desktop/Workspace/Examples/root-tutorials-examples/physics-matters-tutorial/tutorial_4.C:4:10:** **error:** **call to member function 'open' is ambiguous

file.open("data2.txt", ios::in);

~~~~~^~~~

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/fstream:1607:10: note: candidate function

void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);

^

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/fstream:1611:10: note: candidate function

void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out)

^

In file included from input_line_12:1:

/Users/rpaun/Desktop/Workspace/Examples/root-tutorials-examples/physics-matters-tutorial/tutorial_4.C:15:10: error: no member named 'close' in 'std::__1::basic_fstream<char, std::__1::char_traits<char> >'

file.close();

Sorry for not providing you with the full code the first time. What am I doing wrong? Please note I am running this code on a MacOS. Please let me know if you need additional details.

Regards,
Remus

Instead of fstream and open, here’s a simpler version: ifstream file("data2.txt"); Is that any better?

Uhm I’m afraid I am not able to reproduce the problem (i.e. works fine on all systems I could try this on, but admittedly MacOS is not one of them). After all it’s really weird that the compiler thinks that call to open is ambiguous.

Besides what @Axel suggests, can you please try to use .x tutorial_4.C++ with two + signs at the end (it tells ROOT to compile the code using your system’s compiler)? That might help figure out which of the moving parts is causing the problem.

Also: can you call fstream file; file.open("data2.txt", ios::in) in a normal, compiled C++ program?

Cheers,
Enrico

Hello Axel and Enrico,

I used ifstream instead of fstream and it worked. I believe that the problem lies in the interpreter that MacOS uses.
Thank you both for taking the time to solve this.
Regards,
Remus