Hello, I am new to C++ and trying to get to grips with it. Particularly I am having difficulty using both header files and passing ifstream and ofstream types into a function ( i am attempting to kill two birds with one stone). I am getting numerous errors. Any help would be greatly appreciated.
My codes and errors are listed below
Header.h
#pragma once
#ifndef HEADERCODE_h
#define HEADERCODE_H
Error C2065 'ifstream': undeclared identifier
Error C2065 'instream': undeclared identifier
Error C2065 'ofstream': undeclared identifier
Error C2065 'outstream': undeclared identifier
Error C2062 type 'int' unexpected
Error identifier "ifstream" is undefined
Error identifier "instream" is undefined
Error (active) expression preceding parentheses of apparent call must have (pointer-to-) function type ( this is when I call the function write_in_file)
I guess you have made a bit of confusion with what you wrote. Code is a bit messy. I am not an experienced programmer but, according Stroustrup's way, he says it is better if you keep ifs/ofs separated.
You can set up two functions that compile a vector to import and another one to export or simply prints results out.
It is complaining because your header file is not properly structured -- meaning that the compiler does not know what a ifstream is when it first encounters it.
It would work if you were to swap a couple of lines in your .cpp files:
1 2
usingnamespace std;
#include "Header.h"
Rather than do that, though, you should correct your header:
header.h
1 2 3 4 5 6 7 8 9 10 11
#pragma once
#ifndef HEADERCODE_H
#define HEADERCODE_H
#include <fstream>
int factorial(int number);
void write_in_file(std::ifstream& instream, std::ofstream& outstream,int& user_integer);
#endif
You need to #include the headers required for the function prototypes in header.h.
You need to use std:: namespace prefix for C++ library stuff used in header.h.
(Notice also I fixed line 2.)