unresolved external error...???

This is a discussion on unresolved external error...??? within the C++ Programming forums, part of the General Programming Boards category; For some reason when this code gets include i get weird linker error...... here's main function:.. Code: #include <iostream.h> #include ...

  1. #1
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279

    unresolved external error...???

    For some reason when this code gets include i get weird linker error......

    here's main function:..

    Code:
    #include <iostream.h>
    #include "ItemManager.h"
    
    
    int main()
    {
      ItemManager SomeItem;
      SomeItem.ItemCreate();
    
      return 0;
    
    
    }
    here's what the linker is flagging...

    [Linker Error] Unresolved external 'ItemManager::ItemCreate()' referenced from C:\PROGRAM FILES\BORLAND\CBUILDER5\PROJECTS\INVENTORY\MAIN2.O BJ

    just for reference here's the code for ItemManager.h.....

    Code:
    //----------------------------------------------------------------------------
    // Preprocessor directives...
    
     #ifndef ITEMMANAGER_H
     #define ITEMMANAGER_H
    
    //-----------------------------------------------------------------------------
    // class ItemManger
    
     class ItemManager
      {
        public:
    
               void ItemCreate();
    
    
      };
    
    //-----------------------------------------------------------------------------
    
     #endif
    i can't spot the problem....the code compiles fine... linker is the only objective part...

    Thanx in adnvance....

    matheo917

  2. #2
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Re: unresolved external error...???

    Code Fixed:
    Code:
    #include <iostream.h>
    
    #ifndef ITEMMANAGER_H
    #define ITEMMANAGER_H
    
    struct someitem
    {
      int itemcreate;
    };
    
    int main()
    {
      someitem.itemcreate;
    
      return 0;
    
    
    }
    That should work

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Where's ItemManager.cpp?


    Code:
    void ItemCreate();
    This is a function prototype, not a declaration. You cannot call it before defining it.

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    ItemManager.cpp is in a separate file.. i didn't include it in this post....

    so far i figured out that when ItemCreate() function is implemented in a separate file that's when that unresolved external error occurs.... i wonder why?

  5. #5
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    does itemmanager.h #include "itemmanager.cpp" at the bottom?

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    hmmm.....no it doesn't include anything on the bottom... what do you mean?? that's the first time i hear about this...

    ???

  7. #7
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well then that is your problem.

    Here is the deal on class files:

    There are two files. An interface (.h) and an inplementation (.cpp).

    The interface file contains all the function declarations, basically what you would normally find above the int main in a primary file. It might also define a few constants. It is heavily commented, and meant for users of that class to look at. It generally contains no code, no algoriths or anything like that, that is in the implementation file.

    The implementation file is where a class' guts are. All the previously declared functions must be defined, that is, they must be coded.


    Now, to use a class in a program, the .h file is #include'd at the top. I see you have done that.

    However, the (.h) file alone is not enough for the class to function. You need the definitions. So, at the very bottom of your .h file, just above the #endif, you need to #include your .cpp file, so the program will have access to those definitions.

  8. #8
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    thank you..........it woooooorks....
    i am beat...... such trivial thing and yet gives me a lot of headaches....

    hope that reference i posted helped you..

    Regards,
    matheo917

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    That's the first time I've ever heard of doing that. Is that Borland specific? I use MSVC 6.0, learned mostly from Deitel's text, never seen #including .cpp's or #includes at the bottom.

  10. #10
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Nope, its the language.

    The only time you would need to do it is if you wrote your own class and then compartmentalized it into it's own files. That is probably why you haven't seen it.

    Alternatively, if you have taken AP computer science, like alot of people on this board, myself included, have or are, you will have been exposed to the less preferred style of #including the .h at the top of the .cpp and then #including the .cpp in the main program. The case study uses these type of files, although they are considered bad form.

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Actually, the way I've seen it (see Deitel) is to #include the .h file in the .cpp implementation file, then #include the original .h file in the file with main().
    MSVC puts this stuff in a project, or workspace, so maybe it compiles together that way. Never seen your way. Sometimes I mix up w/Java though.

  12. #12
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Well I don't use msvc, and I'm not exactly familiar with the preprocessing commands, but I am pretty sure the "projects" method is an implementation-defined method of using multiple source files.

    The language itself has no notion of a project. I think the diff is that projects are all compiled separately and then brought togther by the loader(linker) whereas the Standard-defined method of using preprocessor directives causes the files to be included by the preprocessor before compilation, then all compiled into one big object file. I prefer the Standard's method, because using project files makes your code needlessly implementation-dependant.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. Help with error
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 04-17-2002, 10:36 AM

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