How do I include .cpp files in my main?

This is a discussion on How do I include .cpp files in my main? within the C++ Programming forums, part of the General Programming Boards category; I have an assignment due in 15 minutes, and I can't figure this one out: I have main.cpp , SortedType.cpp, ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    3

    Question How do I include .cpp files in my main?

    I have an assignment due in 15 minutes, and I can't figure this one out:

    I have main.cpp , SortedType.cpp, ItemType.cpp, SortedType.h and ItemType.h

    SortedType needs the class definitions from ItemType...

    What do I put at the top of my main file so that all these programs are included????

    I thought it would be this, but I am wrong:
    #include "ItemType.h"
    #include "ItemType.cpp"
    #include "SortedType.h"
    #include "SortedType.cpp"

    PLEASE HELP IF POSSIBLE, ITS A SIMPLE PROBLEM, OUR PROF NEVER TOLD US HOW TO DO THIS WITH 5 FILES B4.
    THANKS.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    It could depend on where the files are.... If they're in you include directory, try it with <> instead of ""

    Don't yell.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Ya sorry bout the caps, I was desperate.

    The files are all in a separate directory that I use to keep my programs for school... does that help?

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    3
    Is there a problem in C++ with you #includeing a file more than once??
    cos each of my .cpp files needs the .h file of the same name.

    but my main file will need all 4, correct?

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    What would really help is the error messages you're getting...

    One try (read: shot in the dark) could be to include the path in your include statements...

    #include "C:\\foo\\mydir\\this.h"

  6. #6
    Unregistered
    Guest
    you don't typically list cpp files with the include preprocessor command. You just list the associated header file and the linker will link the cpp file to your project for you.

    I believe the rule is if the header file is in the same directory as the executable file for the program then use the double quote syntax for the include command. Otherwise use the angled bracket syntax.

  7. #7
    Unregistered
    Guest
    you cannot include a .cpp file.

    itemtype.cpp must #include "itemtype.h"
    sortedtype.cpp must #include "sortedtype.h"
    main.cpp must include both .h files in the same way.

    the "" searches the current working directory for the .h file

  8. #8
    Unregistered
    Guest
    Great, thanks a lot for all your help. I finally got it to work with a bit of trial and error... turned out i #included the itemtype.h in the sortedtype.h, and so in my main file, i only had to include "sortedtype.h"

    Thanks again.

  9. #9
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    you cannot include a .cpp file.
    Yes you can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  3. Multiple .cpp files
    By publikum in forum C++ Programming
    Replies: 16
    Last Post: 08-24-2005, 12:56 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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