include's

This is a discussion on include's within the C++ Programming forums, part of the General Programming Boards category; (Can/How do) you include a header file into two different functions that will be compiled into the same program?! ex... ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    17

    include's

    (Can/How do) you include a header file into two different functions that will be compiled into the same program?! ex...


    // main.c

    #include <iostream>

    #include "cat.h"

    using namespace std;

    int main()
    {
    cat spot;
    spot.age = 2;
    printage(spot);
    return 0;
    }


    //printage.c

    #include cat.h"
    #include <iostream>

    using namespace std;

    void printage(cat fluffy)
    {
    cout << fluffy.age << '\n';
    return;
    }


    //cat.h

    class cat
    {
    int age;
    };

    cat::cat()
    {
    age = 0;
    }


    When this is compiled, it throws out the errors:

    /tmp/ccN6V3Pd.o: In function 'cat::cat[not-in-charge]()':
    /tmp/ccN6V3Pd.o(.text+0x0): multiple definition of 'cat::cat[not-in-charge]()'
    /tmp/ccQuUmOM.o: (.text+0x0): first defined here
    /tmp/ccN6V3Pd.o: In function 'cat::cat[in-charge]()':
    /tmp/ccM6V3Pd.o(.text+0x68): multiple definition of cat::cat[in-charge]()'
    /tmp/ccQuUmOM.o(.text+0x68): first defined here
    collect2: ld returned 1 exit status


    The command I use to compile is: g++ main.c printage.c -o main
    I'm using RedHat 8.0 with gcc3.2.
    -----------------------
    R. D. Nichols, Jr.

    -Why do I have to go EVERYWHERE in this handbasket?

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You need to use include guards so that the code isn't included twice.

    For example, cat.h should look like this...

    Code:
    #ifndef CAT_H
    #define CAT_H
    
    // rest of cat.h
    
    #endif
    While that will allow you to have multiple include files, you should really learn about the linking process, which is even more complicated . This way, you can have multiple SOURCE files.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    17
    Thanks, that worked. Do you know where I can get the information you mentioned about the linking process?
    -----------------------
    R. D. Nichols, Jr.

    -Why do I have to go EVERYWHERE in this handbasket?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 11:24 AM
  2. Makefile and includes
    By Lang in forum C Programming
    Replies: 4
    Last Post: 03-18-2008, 04:29 AM
  3. Avoiding multiple includes
    By plan7 in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 06:13 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  5. Circular includes
    By ventolin in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2004, 06:43 PM

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