These days I've started to check out a few things about the C++ support of the CodeWarrior (V. 5.7.1844). I created a small C++ Project and I tried to get the following to run:
#include <iostream>
#include <fstream.h>
using namespace std;
void readFile()
{
fstream inOut("test.txt", ios::in | ios:ut);
}
I always get the Compile Error message: "Error: too many initializers"
If I remove the openemode flags it normally should compile,too , because there is a default parameter in the constructor, but this:
fstream inOut("text.txt");
delivers the compile error:
"Error: implicit conversion from 'const char[9]' to 'std::basic_fstream<char, std::char_traits<char>>'
I have checked all the target settings and the include file contents, but i cannot find a solution to solve the compile errors.
Has anybody an idea ?
Thanks in Advance
mama
use #include <fstream> and not the pre-ansi #include <fstream.h>.
According to
http://www2.roguewave.com/support/docs/leif/sourcepro/html/stdlibref/basic-fstream.html
The constructor you are trying to use is explicit, so you have to call it explicitely.
Without having it tried:
fstream inOut = fstream("text.txt");
Daniel
Hello Daniel,
thanks for your quick answer and sorry for my stupid typing error about the fstream.h extension. Yes of course, I have included it this way in my test programm: #include<fstream> (next time I better do copy and paste), but it does not change the compile error message. Here I have another code snippet below where I tried to get it compilable, but I am not able to solve the problem. You're suggestion about the constructor call delivers another compile error ?
I will be very thankful for advises.
Thanks Marc
// fstream is just forwarded to basic_fstream in file "iosfwd"
template <class charT, class traits = char_traits<charT> >
class basic_fstream;
typedef basic_fstream<char, char_traits<char> > fstream;
// The called constructor basic_fstream in file "fstream"
explicit basic_fstream(const char* s,
ios_base:penmode mode = ios_base::in | ios_base:ut);
---------------------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include "test.h"
using namespace std;
void readFile()
{
// Error : too many initializers
// test.cpp line 14 fstream inOut("test.txt", ios::in | ios:ut);
fstream inOut("test.txt", ios::in | ios:ut);
// Error : illegal use of incomplete struct/union/class
// 'std::basic_fstream<char, std::char_traits<char>>'
// test.cpp line 18 fstream inOut2 = fstream("test.txt");
fstream inOut2 = fstream("test.txt");
// Error : illegal use of incomplete struct/union/class
// 'std::basic_fstream<char, std::char_traits<char>>'
// test.cpp line 22 fstream inOut3 = fstream("test.txt", ios::in | ios:ut);
fstream inOut3 = fstream("test.txt", ios::in | ios:ut);
const char c[] = "text.txt";
ios_base:penmode mode = ios_base::in | ios_base:ut;
// Error : template argument list expected
// test.cpp line 35 explicit basic_fstream(const char* s,
// ios_base:penmode mode = ios_base::in | ios_base:ut);
fstream inOut4 = fstream(c, mode);
}
Oh what a disaster. Now I found the reason for the compile errors by using fstream( ).
I wondered why it makes no difference if I #include<fstream> or not. The reason is
that the basic_fstream class is commented out by preprocessor define _MSL_NO_FILE_IO
in the include file <fstream>.
So the compiler sees just the following forward declaration.....
// fstream is just forwarded to basic_fstream in file "iosfwd"
template <class charT, class traits = char_traits<charT> >
class basic_fstream;
typedef basic_fstream<char, char_traits<char> > fstream;
.....but the implementation of class basic_fstream is missing.
So I thought no problem...comment out #define _MSL_NO_FILE_IO an it works.
No way, when I comment it out I got other compile error in Metrowerks MSL library:
----------------------
Error : undefined identifier 'fclose'
(point of instantiation: 'readFile()')
(instantiating: 'Metrowerks::c_filebuf<char, std::char_traits<char>>::~c_filebuf()')
(instantiating: 'Metrowerks::c_filebuf<char, std::char_traits<char>>::close()')
msl_c_filebuf line 323 if (_CSTD::fclose(file_) != 0)
(corresponding point of instantiation for 'Metrowerks::c_filebuf<char, std::char_traits<char>>::close()')
msl_c_filebuf line 141 }
(corresponding point of instantiation for 'Metrowerks::c_filebuf<char, std::char_traits<char>>::~c_filebuf()')
test.cpp line 30 }
----------------------
Either the fstream support does not work when using MSL with E68k, or I have to activate/deactivate other precompiler seeting here. Has any body of you experience with this kind of problem.
Thanks in Advance
Marc
P.S. Info from MSL C++ Manual:
_MSL_NO_FILE_IO: This flag allows you to turn off file support while keeping memory mapped streams (stringstream) functional.