-
Compiling with headers
This may seem like a basic question.
I am reading c++ without fear and I am near the end. He switches from writing all programs in one file to using include directives.
While I understand how they work, I am confused about how to create a header file and include it.
I am working on inheritance and I need to put a Fraction class in a header file, and then include it in a new file for a subclass.
The explanation in the book is not much more than, type #include "file.h"
I am using codeblocks if that helps. I used there wizard to create a header file named fraction.h, I saved that in the same folder where the file I am working on is save as .cpp.
Using #include "Fraction.h" and trying to compile and run leads to an error of :No such file or directory.
Other than this hang up the book is a smooth learning experience. I am wondering if I should learn to manually compile and remove the IDE to get a better feel for how everything works.
-
Speaking as someone who has done exactly that with Code::Blocks, if that is what you had done then it would have worked. Therefore, you must have actually somehow done something else. First thing I would check is that you spelled the name of the file correctly. If you're on Windows, then Fraction.h and fraction.h should be the same; if you're on Mac or *nix then they are not the same file.
Also if you have more than one .cpp file make sure they are all in a project.
-
Well I got it working, when using headers, does it matter where they are saved?
Also, In my fraction class from the book, I created a member function as
Code:
friend ostream &operator<<(ostream &os, Fraction &fr);
for easy printing of fractions. I was unable to get the code to work until I commented out all pieces containing the above friend function.
When that was not commented and I tried to compile I get the error:
Code:
Floatfract1\fract.h|73|error: ISO C++ forbids declaration of 'ostream' with no type|
Floatfract1\fract.h|73|error: 'ostream' is neither function nor member function; cannot be declared friend|
Floatfract1\fract.h|73|error: expected ';' before '&' token|
Floatfract1\fract.h||In member function 'void Fraction::set(int, int)':|
Floatfract1\fract.h|14|error: 'normalize' was not declared in this scope|
Floatfract1\fract.h||In constructor 'Fraction::Fraction(int, int)':|
Floatfract1\fract.h|32|error: 'normalize' was not declared in this scope|
||=== Build finished: 5 errors, 0 warnings ===|
not quite sure why this error occurs, if I simply paste the fraction class in instead of referring with a header it runs perfectly fine.
Also, thanks for the quick response.
-
Headers need to be saved where the compiler is going to look for them. Generally that's the current working directory for files included inside "quotes", but you can specify any place you want, by putting it in your compiler options (there should be a spot for "include directory"). (EDIT: Looking through C::B again, they call it "search directory" on the compiler settings page.)
As to the second part, I'm not sure what you mean exactly. Did you ever write the operator<< function, or just declare it? If you didn't write it, then any calls to it will naturally fail. If you did write it, then what error messages were you getting?
-
I did write it, originally I had it exactly the same as the book,
a fraction header file, included in two other files, then one file containing fraction function definitions, and a final one with main and the sub class float fraction.
The declaration is in the header file exactly how I pasted it with a definition in another file.
definition:
Code:
ostream &operator<<(ostream &os, Fraction &fr)
{
os << "(" << fr.num << "/" << fr.den << ")";
return os;
}
I tried inlining the definition into the header file and it still reported the same errors.
errors are:
Floatfract1\fract.h|73|error: ISO C++ forbids declaration of 'ostream' with no type|
Floatfract1\fract.h|73|error: 'ostream' is neither function nor member function; cannot be declared friend|
Floatfract1\fract.h|73|error: expected ';' before '&' token|
Floatfract1\fract.h||In member function 'void Fraction::set(int, int)':|
Floatfract1\fract.h|14|error: 'normalize' was not declared in this scope|
Floatfract1\fract.h||In constructor 'Fraction::Fraction(int, int)':|
Floatfract1\fract.h|32|error: 'normalize' was not declared in this scope|
Floatfract1\main.cpp||In function 'int main()':|
Floatfract1\main.cpp|98|error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Value of ")) << fract1'|
-
Either you need to put in using namespace std, or use the actual name, std::ostream.
As to normalize, I can't say what you've done there; perhaps used a function before it was declared.
-
Awesome thanks so much, I was missing #include <iostream> and using namespace std; in the header file.
Thanks so much for helping me out with something thats fairly basic, I really appreciate it.
-
Using "using namespace std;" in headers is a bad idea™. Basically it will force whoever is including that file to live with that namespace declaration. Therefore, it is recommended to use full qualified namespaces in headers and use namespace declarations in source files.
Also note that you operator << should be
std::ostream& operator<<(std::ostream& os, const Fraction& fr)