compiler does not recognize ofstream

I'm currently working on a program to create special lists of people which should be written into files. I'm still quite at the beginning because i was not able tho compile the code below because the compiler gave out the error message : no matching function for call to 'std::basic_ofstream<char>::open(std::string&)' (line 58)

I'm using Dev-C++ 5.8.2 .

Can please help me finding the error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 #include <iostream>
 #include <string>
 #include <fstream>

 using namespace std;
 
 struct tma  //teilnehmende Mitarbeiter = tma 
 	{
		char Vorname[15];
 		char Nachname[15];
 		int marker;
 	};
 	
 int menu()
 {
 	int n=0;
 	char e;
 	system ("CLS");
 	cout << "\n\n\t\t  Menue \n\n";
 	cout << "\t 1 \t Mitarbeiter eingeben \n";
 	cout << "\t 2 \t Mitarbeiter loeschen \n";
 	cout << "\t 3 \t BD - Liste erstellen \n";
 	cout << "\t 4 \t Programm beenden \n";
 	cin >> e;
 	n = e - 48;
 	while (n<1 || n>4)
 		{
 			cout << "\n falsche Eingabe";
 			cout << "\n Bitte noch einmal! \n";
 			cin >> e;
 			n = e - 48;
 		}
 	return n;
 }
 
 int main ()
 {
 
 	tma MA;
 	int n=0;
 	char c;
  	string filename="liste.txt";
 	
 	while (n != 4)
 		{
 		n = menu();
 		switch (n)
 			{
 			case 1:
 				// Eingabe 
 				cout << "\n Vorname : ";
 				cin >> MA.Vorname ;
 				cout << "\n Nachname : ";
 				cin >> MA.Nachname ;
 				MA.marker = 0 ;
 				// in Datei schreiben
 				ofstream myfile;
				myfile.open(filename);
 				myfile << MA.Nachname <<"," << MA.Vorname <<   "\t" << MA.marker << "\n";
 				myfile.close(); 
 				cout << "\n";
 				cout << MA.Vorname << " " << MA.Nachname << " wurde in die Liste eingetragen ! " << "\n\n";
 				system ("PAUSE");
 				break;
 			case 2:
 				// Löschen oder bearbeiten
 				cout << "Loeschen ist noch nicht moeglich!" << "\n";
				system ("PAUSE");
 				break;
 			case 3: 
 				// Liste erstellen
 				cout << "Listenerstellung ist leider noch nicht moeglich!" << "\n";
 				system ("PAUSE");
 				break;
 			case 4:
 				// Programmende
 				cout << " Auf Wiedersehen!" << endl;
 				system ("PAUSE");
 				break;
 		
 			}
 		}
 	cout <<  "Programmende!";
	return 0;
 }
Last edited on
It should be myfile.open(filename.c_str());
With compiler option -std=c++11 it should work with either a std::string or a char * for the filename.
Thank you for your fast answers.

But probably i do not understand it correctly.

I thought that filename is already a string. In the tutorial the filename was even used directly. ( But it didn't work in my case as well)


If i replace myfile.open(filename); with myfile.open(filename.c_str()); i get another error from the compiler: crosses initialization of 'std::ofstream myfile'



crosses initialization of 'std::ofstream myfile'

This can be a little hard to understand when beginning with C++. Within the switch statement, usually the code for each case ends with a break statement, in order to stop execution from 'falling through' to the next case(s). In a somewhat similar way, when a variable is declared within a switch statement, it can be accessed at any point in the following cases. That can cause problems.

The simplest solution is to enclose the code for the case between open and close braces { }. That limits the scope of the variables to just the part where they are used.

Something like this:
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    case 1:
        {
            // Eingabe 
            cout << "\n Vorname : ";
            cin >> MA.Vorname ;
            cout << "\n Nachname : ";
            cin >> MA.Nachname ;
            MA.marker = 0 ;
            // in Datei schreiben
            ofstream myfile;
            myfile.open(filename);
            myfile << MA.Nachname <<"," << MA.Vorname <<   "\t" << MA.marker << "\n";
            myfile.close(); 
            cout << "\n";
            cout << MA.Vorname << " " << MA.Nachname << " wurde in die Liste eingetragen ! " << "\n\n";
            system ("PAUSE");
        }
        break;

The other cases are ok, as there are no variables being declared.
Thank you very much, now it's working.
You really saved my day.
Topic archived. No new replies allowed.