how to pass an ifstream object to a function

This is a discussion on how to pass an ifstream object to a function within the C++ Programming forums, part of the General Programming Boards category; hi masters!! can anyone plese help me in this? i want to pass an ifstream object to function. please dont ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    78

    how to pass an ifstream object to a function

    hi masters!!

    can anyone plese help me in this?
    i want to pass an ifstream object to function.

    please dont suggest me to use istream instead!!!!!!!

    i know it can b passed by reference, but i am not been able to do that!!

    my code
    Code:
    #pragma warning(disable:4786)
    #pragma warning(disable:4996)
    
    #include <vector>
    #include "common.h"
    #include "defFile.h"
    #include<uf.h>
    #include <iostream>
    #include <uf_part.h>
    #include <uf_curve.h>
    #include"runner_blade.h"
    
    using namespace std;
    void defFile::readFile(char *DefFileName)
    { 
    	char buf[256];
    	int i=0,j=0;
    	ifstream inpfile;
    	inpfile.open(DefFileName);
    	while(!inpfile.eof())
    	{
    		inpfile.getline(buf,256,'\n');
    		if(strstr(buf,"action"))
    		{
    			//I'm check whether the string action is coming or not		
    			inpfile.getline(buf,256,'\n');		
    			sscanf(buf,"%d %d",&ActionSide.NumberOfSections,&ActionSide.NumberOfPoints);
    			readLine(ActionSide.NumberOfSections,ActionSide.NumberOfPoints,ifstream &inpfile);			
    		}
    	}
    	inpfile.close();
    }





    error:


    1>d:\backup_qi-287\geconfidential\rajesh\hitachi_runner_blade\cod e_runner_blade\runner_blde\runner_blde\deffile.cpp (28) : error C2275: 'std::ifstream' : illegal use of this type as an expression

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    It sounds like you need to #include <fstream>

    please dont suggest me to use istream instead!
    If I suggest using an ifstream, I'm suggesting to use an istream, since an ifstream is an istream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    thanks for suggestion but still its not working & i think u misunderstood me ! i want to use "ifstream " . i dont want to use "istream"!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    thanks for suggestion but still its not working
    Ah, but what is the error message now? I suspect it has changed, because the next problem you have appears to be here:
    Code:
    readLine(ActionSide.NumberOfSections,ActionSide.NumberOfPoints,ifstream &inpfile);
    If readLine() takes an ifstream (or istream, for that matter) by reference, you would write:
    Code:
    readLine(ActionSide.NumberOfSections, ActionSide.NumberOfPoints, inpfile);
    i think u misunderstood me ! i want to use "ifstream " . i dont want to use "istream"!
    I understand you very well. You, on the other hand, do not understand that there is an is-a relationship between ifstream and istream, and consequently you should prefer to use reference to istream parameters unless ifstream specific functionality is to be used.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    hi expert!! i have done whatever u said & i am getting this error!! u have to b very patient with me, because i am begineer!!!

    thanks!!



    error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
    On which line is that?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    i cant tell , it directly goes to fstream!!!

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    i cant tell , it directly goes to fstream!!! when i click on error

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Take a look at your previous error message:
    d:\backup_qi-287\geconfidential\rajesh\hitachi_runner_blade\cod e_runner_blade\runner_blde\runner_blde\deffile.cpp (28) : error C2275: 'std::ifstream' : illegal use of this type as an expression
    See the part that I bolded? That's the line number. Look for that in your current error message.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    sorry went for lunch, i am not been able to find it .i have pasted whole stack here!!










    1>------ Build started: Project: runner_blde, Configuration: Debug Win32 ------
    1>Compiling...
    1>defFile.cpp
    1>d:\apps\microsoft visual studio 8\vc\include\fstream(675) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> d:\apps\microsoft visual studio 8\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> This diagnostic occurred in the compiler generated function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstrea m(const std::basic_ifstream<_Elem,_Traits> &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1>Build log was saved at "file://d:\Backup_QI-287\GEConfidential\rajesh\Hitachi_runner_blade\cod e_runner_blade\runner_blde\runner_blde\Debug\Build Log.htm"
    1>runner_blde - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Streams are non-copiable and their copy constructors are private for that reason.

    Are you trying to pass a stream to a function by value? You can't do that and must pass a reference.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    hi anon,

    i tried passing it with reference but it is giving below error,


    d:\backup_qi-287\geconfidential\rajesh\hitachi_runner_blade\cod e_runner_blade\runner_blde\runner_blde\deffile.cpp (28) : error C2275: 'std::ifstream' : illegal use of this type as an expression

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The code in the first post?

    Whether something is passed by reference or by value is determined where the function is declared/defined, not where it is called.

    Example of a function that takes a reference.
    Code:
    void foo(std::istream& is); //declaration
    
    //implementation
    void foo(std::istream& is) 
    {
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    anon i dont want to use istream !! i am using ifstream!!!after declaring as u said i get this error!

    error C2275: 'std::ifstream' : illegal use of this type as an expression

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    thanks got it!!!!!!!!!!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21