Compiling with headers

This is a discussion on Compiling with headers within the C++ Programming forums, part of the General Programming Boards category; This may seem like a basic question. I am reading c++ without fear and I am near the end. He ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    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.
    Last edited by bob887; 12-30-2010 at 08:39 PM. Reason: forgot to ask my question.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?
    Last edited by tabstop; 12-30-2010 at 08:42 PM.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    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:perator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Value of ")) << fract1'|

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    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.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,431
    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)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking libraries when compiling with g++ in ubuntu
    By Veni_Vidi_Vici in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2010, 10:01 PM
  2. Compiling headers
    By hello in forum C Programming
    Replies: 3
    Last Post: 01-12-2009, 09:32 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21