Dependency problems and classes!

This is a discussion on Dependency problems and classes! within the C++ Programming forums, part of the General Programming Boards category; Hi, I'll cut to the chase I have the following file: zfx.h which needs definitions from the file zfx3d.h to ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    87

    Wink Dependency problems and classes!

    Hi,

    I'll cut to the chase I have the following file:

    zfx.h
    which needs definitions from the file

    zfx3d.h
    to function. Some of those definitions are classes. Great (not). Unfortunately, very unfortunately, all of my .cpp source files in the project want a copy of zfx.h to work. This means they all get a copy of the definitions in zfx3d.h and then the linker blows up. Fine you might think, just split up the declarations in the header file and then put the definitions in a .cpp file.

    No such luck. Because alot of the functions in zfx3d.h are inline functions and they have to be declared within the same file or the compiler ..........es about 'used but never defined' warnings.

    Forward declarations then perhaps. No such luck, then the compiler moans about finding 'incomplete type' with the zfx.h file.

    What I really need to do is one of two things:

    1) Allow some kind of forward declaration within zfx.h that allows it to compile without having access to a file that contains the class definitions from zfx3d.h - Compiler problem.

    2) Find some way to prevent each and every source file from getting a copy of the definitions within zfx3d.h and making an object of them - Linker problem.

    Is there anything I can do here save putting everything bar one source file into a header file or some other horribly drastic over the top method?

    Like I said I can't split up the declarations from the definitions in the header file as the functions are mostly inline functions. This seems yet another way for C++ to drive you crazy and hold you back even after you've long since understood the code involved and are ready to progress.

    I truly have no idea what to do here, has anyone got any ideas at all?

    Thanks for any help anyone can offer

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Well, unless zfx.h contains inline and/or function definitions, and granted you pass by reference or address, you don't need the complete definitions. Forward definitions will suffice.

    Also, it is curious that you get linker errors when multiple source files include the headers. Did you put "inline" before all function definitions in zfx3d.h?
    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.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    I was hoping very much you in particular would reply. This is a bit more complex than perhaps I had really made clear before. I have had one last idea which maybe can solve this, I'll attempt to recreate and solve the issue in a much smaller set-up and see if it works. If it does I'll post back and say what worked, if it doesn't I'll reply in more detail.

    Thanks

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    I have now found out as much as I would probably ever need to about this. I'll post a complete description of what happened and how I fixed it when I'm not feeling so tired. Probably tomorrow sometime

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    Hi ok this is a brief summary of what happened.

    1)The original author had tried to use the inline keyword in function definitions in a header file, whilst defining those same inline functions in a .cpp file. Not possible. Compiler wants to know there and then the definitions of the functions.

    2)I got away with it to a certain extent but a load of 'used but never defined' warnings popped up when compiling the static library made from these files.

    3)Subsequent attempts to link to functions in this static library in other applications fail due to 'undefined reference' errors. Basically the linker could not find these functions in the static library due to complications mentioned above. I guess some sort of name mangling or functions never even actually being defined at all.

    4)I become aware of what's going on then try to get everything into the one header to preserve the inline capability. This works provided the inline definitions are also in the header file. However having inline functions declared in the header file but outside of the class definition again causes linker errors and multiple identical objects when building.

    5)Ok so still trying to preserve the inline stuff I put the inline function definitions right into the class definitions. This seems logical, but causes compiler errors because forward declarations now break down because most of these inline functions are asking for sensitive and personal information about the classes defined forwardly above. The compiler will tolerate forward declarations, but will not tolerate references to data members and member functions of incomplete (i.e. forwardly defined) classes.

    6)I become aware I am in a vicious circle after much frustration. It appears that to preserve the inline capability, and have functions referencing interdependent classes, you would need to have exactly the right placement combination of entire class definitions complete with function definitions so that the compiler never had to resolve anything it wasn't already aware of.

    7)The 'magic' combination mentioned in point 6) may not even physcially exist and is thus pointless trying to find. I strip the inline keywords away and go back to the original method of header file declarations and source file definitions. The code compiles, links and is operational for testing purposes.

    8)I make peace with the fact that to really get the most of modern processors with a fast math library I will just have to learn to write an alternative code path for SIMD stuff, not just dump inline keywords infront of the functions in the classes of my math library.

    Take note anyone reading, discovering this on my own was not pleasant and lasted several days. Best to stick to established practises!!!

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 12:45 PM
  2. Problems with memory allocation in classes
    By MathFan in forum C++ Programming
    Replies: 6
    Last Post: 01-09-2005, 01:05 AM
  3. Problems with classes... just starting out w/ them.
    By criticalerror in forum C++ Programming
    Replies: 25
    Last Post: 03-05-2004, 11:19 AM
  4. Having big problems with classes
    By Vorok in forum Game Programming
    Replies: 16
    Last Post: 01-11-2003, 02:16 AM
  5. Replies: 2
    Last Post: 01-15-2002, 05:00 PM

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