Thread: problem with header files

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Question problem with header files

    hi!
    I'm separating header and cpp files of my program and I'm facing some problems.
    first: what does defining inline classes in header files mean? does it mean that we include the definition of each function as well as its prototype in the pertinent header file?

    second: I have a separate function which doesn't fit in any of the classes I have defined and 2 global vectors, with one of them of a user-defined type. where should I declare them? in what header or cpp file? (it might be useful to point that the mentioned function uses these vectors)

    third: if defining inline classes in header files is what I have written in number one, I have to use the vectors I've mentioned above, in one of my classes. if I don't define the vectors mentioned in this header file, it will not know the identifiers. I'm getting somewhat confused coz it seems that I have to define these vectors repeatedly in more than one of my header files.

    I'd appreciate it if you helped me out.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Farnaz View Post
    hi!
    I'm separating header and cpp files of my program and I'm facing some problems.
    first: what does defining inline classes in header files mean? does it mean that we include the definition of each function as well as its prototype in the pertinent header file?
    An inline function is one which either has it's definition (not declaration) prefaced with the keyword "inline" or which is defined in the declaration/header as you describe. These can be class methods. Is that what you mean?

    The normative reason to inline is to optimize for speed, but compilers tend to do that anyway if they perceive an opportunity. Template functions/methods are also (usually) defined in headers as inline functions, altho that is sort of a traditional hack solution to a traditional awkward problem.

    second: I have a separate function which doesn't fit in any of the classes I have defined and 2 global vectors, with one of them of a user-defined type. where should I declare them? in what header or cpp file? (it might be useful to point that the mentioned function uses these vectors)
    If the vectors are going to be widely used (eg, in methods of more than one class), declare them in one header normally and in the other headers with the keyword "extern". If they are only used in main(), you could just declare them in that .cpp file, or in a single header for it. In this case a good practice is to declare that variable "static", meaning although it is global, it will not be accessed outside of the file in which it is declared.

    The same logic more or less applies to your non-class functions. If these are used by any of the classes, you need to put them in the appropriate headers with "extern" and then declare them normally in the header for main. Otherwise, you only need to declare them in the main header (you can declare such functions static too).

    Hopefully that makes sense? Just do not declare a global more than once anywhere without "extern" (the compiler will likely throw an error for that anyway).
    Last edited by MK27; 04-01-2011 at 11:49 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    thank you, it helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. Many Header Files
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 03-08-2005, 02:45 PM
  4. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM
  5. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM