Multiple source file with fstream

Sorry for my poor English, basically what i want to do is:

function.h
void function (ifstream& infile);

function.cpp
void function (ifstream& infile)
{
//do something here
}

main.cpp
#include <iostream>
using namespace std;
int main ()
{
ifstream infile;

function (infile);
return 0;
}

Error on line function (infile);


/undefinedReference/main.cpp|25|undefined reference to `function(std::basic_ifstream<char, std::char_traits<char> >&)'|

May i know what is my error code?


Yeah,
you must include the function.h header file with the #include directive, then the linker would "see" the definition of the function.

So:
#include "function.h"

note the "" not <>

Thanks,
Aceix.
Read: http://cplusplus.com/doc/tutorial/preprocessor/ for more information about preprocessor directives.

HTH,
Aceix.
Thank you for your rapid reply Aceix. I had included "function.h" but yet it still not working..
Since you're using file streams, you must include the <fstream> header file.
Also, what are the error(s) you are receiving?

Aceix.
//main.cpp
#include <iostream>
#include <fstream>
#include "function.h"

using namespace std;

int main()
{
ifstream infile;

function (infile);

return 0;
}

//function.cpp

#include "function.h"
#include <iostream>

using namespace std;

void function (ifstream& infile)
{
cout << "You're here! \n";
}

//function.h
#include <iostream>

using namespace std;

void function (ifstream& infile);

Error:
/undefinedReference/main.cpp|13|undefined reference to `function(std::basic_ifstream<char, std::char_traits<char> >&)'|
||=== Build finished: 1 errors, 0 warnings ===|
Last edited on
Topic archived. No new replies allowed.