So im quite new to header files and using more then one file in general. Ive heard that using a header file to place your functions in is a good idea usually so im doing that for a exercise that I have.
The problem that I have run into though is this
Header File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
#include <vector>
#include <iostream>
#include <string>
#include <stdexcept>
#include <algorithm>
// student_info is a stuct in my main.cpp
std::istream &read(std::istream &in, student_info &s)
{
is >> s.name >> s.midterm >> s.final;
read_hw(in, s.homework);
return in;
}
#endif // FUNCTIONS_H_INCLUDED
As you can see im getting a error because student_info isnt declared in this file, and I was wondering if I should #include "main.cpp" or if that would cause problems. Also if I have #include sting and other includes in both my main.cpp and functions.h files will that be a problem?
Thanks for the help with a kind of stupid question.
It is generally a very bad idea to put your function definition in a header. It will cause a linker error if more than one cpp file includes the header (header guards have nothing to do with this particular error).
Put the function declaration in a header file. Put the function definition in one cpp file.
On to your particular problem. Never #include a cpp file. More carefully, don't do it unless you really, really know what you're doing.
Put the definition of the type student_info in a header file, and then #include that header file in each cpp and header file that needs to know what a student_info is.