Thread: deal with multiple includes

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    18

    deal with multiple includes

    I dont get how to avoid multiple includes.
    The task is simple:
    We have 2 (for example) source files and one header file. We want to use this header for both sourse files.
    I have found some explanation of this issue but its hard to understand.
    All I know is :
    1) Using #pragma once is a bad practice for some reason.
    2) I must to use #ifdef (ifndef actually), #define, and #endif.
    But how?! I dont get it...
    Any help is appreciate.
    Last edited by rockdj; 07-29-2004 at 07:20 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    well all you do is this in your header
    Code:
    ifndef _HEADER_H
    #define _HEADER_H
    //prototypes
    #endif//_HEADER_H
    Where HEADER is the name of you .h file. The way i do things is
    you make your header file with your prototypes then you define them in your .cpp file eg:
    Code:
    #ifndef _TEST_H
    #define _TEST_H
    void printNumber();
    #endif//_TEST_H
    Code:
    #include <iostream>
    #include "test.h"
    using std::cout;
    void printNUmber()
    {
       cout<<"20"<<endl;
    }
    Last edited by prog-bman; 07-29-2004 at 07:41 AM.
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    THE SAME example I have in my book! I dont understand what I do wrong.
    _HEADER_H - Is it actually header name or just something? If it is, its not working for me...
    I type:
    Code:
    #ifndef _MAIN_H
    #define _MAIN_H
    void callFunc();
    #endif
    ... and the same error appear on my screen - "one or more multiply defined symbols found"

    OH SHAME ON ME! Do you know what I do wrong? I just made two files with main() function and use both in the same program @_@
    Last edited by rockdj; 07-29-2004 at 08:06 AM.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can't use main twice in the same program. There really is no need to either here let me whip something up for you to look at
    Code:
    //This is the header file which will allow you to call a function
    //defined in another cpp file
    #ifndef _TEST_H
    #define _TEST_H
    int add(int a, int b);
    int subtract(int a, int b);
    #endif//_TEST_H
    Code:
    //This is the cpp file which defines add and subtract
    #include "test.h"
    int add(int a, int b)
    {
      return (a+b)
    }
    int subtract(int a, int b)
    {
      return (a-b)
    }
    Code:
    //this is your main.cpp file
    #include <iostream>
    #include "test.h"
    using std::cout;
    using std::cin;
    
    int main()
    {
      cout<<add(1,9);
      cout<<subtract(9,1);
      cin.get();
      return 0;
    }
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    18
    prog-bman, THANK you! I wonder why its not cover in my book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-16-2008, 03:11 PM
  2. #include's in multiple modules
    By samus250 in forum C Programming
    Replies: 8
    Last Post: 03-24-2008, 02:12 PM
  3. Avoiding multiple includes
    By plan7 in forum C Programming
    Replies: 5
    Last Post: 11-25-2007, 06:13 AM
  4. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  5. multiple header #includes
    By bennyandthejets in forum Windows Programming
    Replies: 2
    Last Post: 12-30-2002, 10:04 PM