C++ - Interface and Implementation in separate files not working?

Posted Tuesday, March 24, 2009 by admin


I’m VERY new to C++, and am using a tutorial e-book. It’s up to the point where I learn about classes. It separates the class into two files, with the interface in the .h file, and the implementation in a .cpp file.

However, when I try to compile the test program it doesn’t work! The interface is ITEM1.h, the implementation is ITEM1.cpp, and I’ve got the #include ITEM1.h in the test program. The error is something to do with StockItem::StockItem() (which is the class, with the default constructor) not being defined correctly. If I copy and paste the stuff from the implementation file into the test program, above main(), it works.

Why isn’t it realizing where the implementation .cpp file is? Is it my fault? A compiler error? Something I haven’t specified? ANY help would be appreciated.

Thanks in advance!
No, it finds the .h file fine, I know this because it gave me a specific error because the name wasn’t capitalised. It’s not understanding the information in the implementation file, the .cpp file. The implementation file is separate from the other cpp file, the test program.
The implementation file contains all the info for stuff such as the member function StockItem.Display(), which displays some variables. In the .h file, it is just listed as void Display(), but in the implementation it’s got all the code needed. The test program references StockItem.Display(), but it doesn’t work.
When I include the contents of the implementation file into the test program, it works!!!!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Ask

2 Comments on "C++ - Interface and Implementation in separate files not working?"

  • The Phlebob said on Mar 24th, 2009 at 7:20 AM:

    This sounds like the preprocessor (the thing that interprets all the # lines, including the #include lines) might not be finding the header (.h) file. Make sure it’s in the correct directory and that the compiler is aware of that directory.

    Also, make sure only ONE #include Item1.h line is being processed. Defining the class twice will cause problems.

    Hope that helps.

  • cja said on Mar 26th, 2009 at 9:28 AM:

    I don’t know what compiler you’re using, but with g++ I’d do this:

    g++ -c ITEM1.cpp
    g++ -o itemTst itemTst.cpp ITEM1.o

    The first line compiles ITEM1.cpp and creates the object file ITEM1.o.
    The second line compiles the test program, and links in ITEM1.o.

    Whatever compiler you’re using, you need to do something similar. The fact that you said it works when you copied the contents of ITEM1.cpp into your test program makes me think that when you had ITEM1.cpp separate you weren’t linking ITEM1.o with your test program. Without seeing your code or development environment, though, it’s hard to say for sure if this is your problem.

Leave a Comment