ifstream error

Compiler Error- No more ideas- need some fresh eyes.

Declaration:
1
2
3
4
5
#include <string>
#include <stdlib.h>
#include <iostream>
  typedef char *charptr;
  char set_initialParameters(charptr filename_ptr);


Definition:
1
2
3
4
5
6
7
8
   char set_initialParameters(charptr filename_ptr){
        string param_line,numberstr,filename;
        char number[12],dataType,filename_array[100];
        for (int i=0;i<100;i++){filename_array[i]=filename_ptr[i];}
        filename=filename_array;
        ifstream file(filename); // error: variable ‘std::ifstream file’ has initializer but incomplete type
...
}


Call:
1
2
3
4
5
    char *initial_param_ptr;
    char file_input[100]="/home/natasha/FWI/FAFS/InitialParameters.txt",dataType;
    initial_param_ptr=file_input;
    
    dataType=set_initialParameters(initial_param_ptr);
Last edited on
have you included:

#include <fstream>
sorry, i should have added that. it is included in the iostream at the top. Anything else you see wrong with this?
ifstream file(filename.c_str());
so you have this??

1
2
3
4
#include <string>
#include <stdlib.h>
#include <iostream>
#include <fstream>  
@m4sterr0shi- tried that- still getting the same error...
@guestgulkan- fstream is in the iostream hierarchy- why include both? isn't fstream inherently included when iostream is?
Last edited on
stavros wrote:
@guestgulkan- fstream is in the iostream hierarchy- why include both?
isn't fstream inherently included when iostream is?

Well, try including it and see if it works... :P My guess is that it will work... ;)
Last edited on
So I did- it seemed to clear up that error (made a bunch of others :-p), but can you explain to me why it isn't included when iostream is?
fstream is a subclass of iostream and so iostream does not need to know anything about fstream.
Wouldn't have thought that it would have made any errors - you probably have other issues in your code which have now come to light.

but can you explain to me why it isn't included when iostream is?
iostream does not include the filestream stuff. It does include a number of forward declarations
which is why you get the 'incomplete type error ' rather than a 'undefined' error.

But the actual class definitions are in <fstream>
I will rephrase what Galik said, using an image :P

http://cplusplus.com/reference/iostream/
Last edited on
ya... the errors are in the code and unrelated, which is why I didn't mention them in any detail. So to get this right, by including fstream I inherently include iostream because fstream needs iostream but iostream DOES NOT need fstream?
There is no guarantee you will get all of iostream if you include fstream. The library producers may seek to keep the inclusions as efficient as possible.

Really, if you include a symbol from iostream then you should #include it and the same for fstream. You should never rely on one header including another.

Otherwise your code can break when you upgrade the libraries or move to a different vendor.
stavros wrote:
by including fstream I inherently include iostream

Actually, no. If you take a closer look in the link I pasted above, you'll see that the classes in <fstream> inherit from the classes found in <istream>, <ostream> and <streambuff>.

EDIT: I mean, this won't work:

1
2
3
4
5
6
7
8
#include <fstream>
using namespace std;

int main()
{
    cout << "asdf" << endl;
    return 0;
}
Last edited on
Thanks all! Problem solved.
Topic archived. No new replies allowed.