473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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)

Nov 22 '05 #1
4 6951
Fa******@gmail.com wrote:
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);

--

Valentin Samko - http://www.valentinsamko.com
Nov 22 '05 #2
Fa******@gmail.com wrote:
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
Nov 22 '05 #3

<Fa******@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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-lit....html#faq-27.5

Regards,
Sumit.
--
Sumit Rajan <su****@msdc.hcltech.com>
Nov 22 '05 #4
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

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: John Tiger | last post by:
Can anybody have idea about the difference between #include <iostream.h> and #include <iostream>. Is later one valid statement on any compiler. I tried compiling on MSVC second statement give...
12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
8
by: Christopher Benson-Manica | last post by:
I know I keep asking similar questions, but I really want to do this at least sort of right. Not to mention I got no on-group replies to my previous post :( I desperately want an interface that...
11
by: Charles L | last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of <iostream.h> unnecessary. Is this correct? Charles L
1
by: Macca | last post by:
Hi, I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to text files. I have now started to use vectors and when i added #include <vector> using namespace std; to...
0
by: Steven T. Hatton | last post by:
I copied what I believe is a complete representation of <iosfwd> from ISO/IEC 14882:2003(E). My understanding of the rules covering typedefs, templates and forward declarations is not very solid. ...
4
by: nils | last post by:
Hi all, Recently I started migrating a gcc project to Visual Studio C++ (dotnet). The problem is that I cannot include any iostream header-file: #include <fstream> int main (int argc, char *...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
4
by: james.lawton | last post by:
Hi, I'm having a problem that I can't diagnose. I'm creating istreams of unknown types, and want to manage them on the stack, co I'm passing around ownership-holding pointers. Usually, I would...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.