The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 12 of 12

Thread: Header Help

  1. #1

    Header Help

    Hey, I'm just starting to learn C++ and I'm using a Dummies Guide book. It says that I need to make a header file and use #include "header.h" for the linking. So I created a new source file called header.h, but when I compile it I get an error saying that there's a Linker error and that there's an undefined reference to all the variables that are set in the header file. Is this because I'm not making the header file correctly? Help is appreciated.
    Slinky + Escalator = Everlasting Fun!

  2. #2
    Could you post your source code please? It's kind of hard to tell what's going wrong without seeing code. However, you probably are doing something wrong in your header file.

  3. #3
    Also, please post the command you’re using to compile; the wrong arguments can get it very confused.


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  4. #4

    Code

    main.cpp
    #include <cstdlib>
    #include <iostream>
    #include "header.h"
    using namespace std;
    int main(int argc, char *argv[])
    {
    variable = 20;
    function();
    system("PAUSE");
    return 0;
    }
    proto.cpp
    #include <cstdlib>
    #include "header.h"
    #include <iostream>
    using namespace std;
    void funtio() {
    cout << "Some random words." << endl;
    cout << variable << endl;
    }
    header.h
    extern int variable;
    void function();
    Slinky + Escalator = Everlasting Fun!

  5. #5
    Quote Originally Posted by DanDaMan View Post
    Code:
    void funtio() {
    That should be function().

  6. #6
    Whoops. Thanks but I still get the same error message. Why?
    Slinky + Escalator = Everlasting Fun!

  7. #7
    actionAction's Avatar
    1,142
    posts
    humanBeing._beard=true;
    You are not including the proto.cpp file anywhere. Change it to proto.h, move it to the headers folder (assuming you are using VS) and include it in your main.cpp file.

  8. #8
    But, proto.cpp is fine. Everything is working the way it should be. Except for the fact that I made a source file called header.h and for some reason main.cpp doesn't recognize anything that was set in header.h. I'm using Dev-C++ 4.9.9.2.
    Slinky + Escalator = Everlasting Fun!

  9. #9
    Have you added the header.h to the dev-c++ project? Otherwise it probably won't compile and link against it, which might be what your dealing with.

  10. #10
    Yeah, I did. I started playing around with it and somehow I fixed it. I have no idea what I did lol. Thanks for your help, everyone
    Slinky + Escalator = Everlasting Fun!

  11. #11
    Quote Originally Posted by actionAction View Post
    You are not including the proto.cpp file anywhere. Change it to proto.h, move it to the headers folder (assuming you are using VS) and include it in your main.cpp file.
    That's the worst advice I've seen on here for a long time.

  12. #12
    it seems like your defining the variable 'variable' in the main function block, it should be global for the extern to be able to see it. Just initiate it outside of the main function block. You should also declare the variable in every seperate file that it occurs in, which means also in the proto.cpp file.

    Main.cpp:

    Code:
    #include <cstdlib>
    #include <iostream>
    #include "header.h"
    using namespace std;
    
    // move variable outside main block function and decalre it again since its in a seperate file
    int variable = 20;
    
    int main(int argc, char *argv[])
    {
    function();
    system("PAUSE");
    return 0;
    }
    proto.cpp:

    Code:
    #include <cstdlib>
    #include "header.h"
    #include <iostream>
    
    // also declare variable here as extern because it is used in this file
    extern int variable;
    
    using namespace std;
    void funtion() {
    cout << "Some random words." << endl;
    cout << variable << endl;
    }
    havent tested it but it looks okay to me.
    Last edited by dozza92; August 27th, 2008 at 09:32 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012