Can't use <fstream> functions in a class?

F

Falos425

Okay, I've got a driver.cpp, a section.cpp, and a section.h because
I've got a 'section' class.

I have an #include <fstream> in the section.h and then section.cpp has
an #include "section.h"

Trouble is, one of the class functions is supposed to (homework) take a
string as a parameter, and use it as the filename. So in the function,
I've got..

void Section::readData (char* fileName)
{
ifstream gradesFile(fileName);

....and get a compiler error. I'm fairly sure #include <fstream> is in
the proper place, so why do I keep getting

section.cpp: In member function `void Section::readData(char*)':
section.cpp:28: error: `ifstream' undeclared (first use this function)
 
V

Valentin Samko

Okay, I've got a driver.cpp, a section.cpp, and a section.h because
I've got a 'section' class.

I have an #include <fstream> in the section.h and then section.cpp has
an #include "section.h"

Trouble is, one of the class functions is supposed to (homework) take a
string as a parameter, and use it as the filename. So in the function,
I've got..

void Section::readData (char* fileName)
(const char* fileName)
or
(const std::string& filename)
would be better, as this explicitly states that you do not intend to change the passed
string in your function.
{
ifstream gradesFile(fileName);
ifstream is in the std namespace, i.e. either you need to bring std::ifstream into the
current scope (for example by "using std::ifstream"), or just explicitly write

std::ifstream gradesFile(filename);
 
J

John Ratliff

Okay, I've got a driver.cpp, a section.cpp, and a section.h because
I've got a 'section' class.

I have an #include <fstream> in the section.h and then section.cpp has
an #include "section.h"

Trouble is, one of the class functions is supposed to (homework) take a
string as a parameter, and use it as the filename. So in the function,
I've got..

void Section::readData (char* fileName)
{
ifstream gradesFile(fileName);

...and get a compiler error. I'm fairly sure #include <fstream> is in
the proper place, so why do I keep getting

section.cpp: In member function `void Section::readData(char*)':
section.cpp:28: error: `ifstream' undeclared (first use this function)

Well, first, a char* is a bad choice for a string. A std::string is
better in C++. And it really should be a const char * if you have to use
a char ptr. Use a std::string if you can, or at least change it to a
const char *.

Second, did you put a using declaration? You need to have one of the
following:

using namespace std; // not really the best style-wise
using std::ifstream; // acceptable, but probably not really necessary
std::ifstream gradesFile(fileName); // best solution

If it doesn't work, post your full code.

--John Ratliff
 
S

Sumit Rajan

Okay, I've got a driver.cpp, a section.cpp, and a section.h because
I've got a 'section' class.

I have an #include <fstream> in the section.h and then section.cpp has
an #include "section.h"

Trouble is, one of the class functions is supposed to (homework) take a
string as a parameter, and use it as the filename. So in the function,
I've got..

void Section::readData (char* fileName)
{
ifstream gradesFile(fileName);

...and get a compiler error. I'm fairly sure #include <fstream> is in
the proper place, so why do I keep getting

section.cpp: In member function `void Section::readData(char*)':
section.cpp:28: error: `ifstream' undeclared (first use this function)

You will need to show us a more complete version of your code.

There is a possibility that you have not taken into consideration that the
standard library is defined in a namespace called "std". So you will need to
qualify "ifstream" with "std::".

Also see:
http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

Regards,
Sumit.
 
F

Falos

Yes, it turned out I had no std:: application. We've been taught to use
"using namespace std;" so far, but it didn't apply to section.cpp from
driver.cpp

An honest thanks for all the efforts from everyone. ~Falos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top