Thread: includein cpp file in a cpp file

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    includein cpp file in a cpp file

    I read this:
    http://cboard.cprogramming.com/showthread.php?t=31408

    In main.cpp if i write
    #include "Point.h"
    #include <iostream>
    #include "Point.cpp"//also i add cpp file

    comiler gives me:
    "'Point' : 'class' type redefinition " error.But i don't understand it.In Point cpp there is no definetion but it gives error.What is wrong here?What is happening while compiling and linking so it gives error.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Europe/Belgrade
    Posts
    78
    You have definition of class Point methods in two places: in the Point.cpp and in the main.cpp where you included it.
    Don't include .cpp files, link them.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In addition, the header file needs include guards because it will be included by both cpp files that are compiled separately. Not having the include guards will cause the redefinition error even when you don't include the cpp file if you are compiling both sources correctly.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Thanks
    Yes daved you are right , i put include guards but it still gives error.But i don't understand your explanation.Can you please explain it again.
    Thanks.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    This is a thread that may help you understand better how you should structure your files.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Are you building a project with Point.cpp and main.cpp? If so then your problem is likely that, since everything in Point.cpp is getting appended to main.cpp in the preprocessor directive, then it's like having everything in Point.cpp defined twice. That's your multiple definitions. Or so I would guess.
    Last edited by SlyMaelstrom; 06-15-2006 at 11:00 AM.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> i put include guards but it still gives error.
    You should not be including a cpp file in another cpp file. My advice to use header guards was in addition to the advice to not include a cpp file.

    What you should do is structure your code exactly as it is in the thread you linked to, except add include guards to Point.h. Then, compile both Point.cpp and main.cpp separately and link them together. If you are using VC++ or Dev-C++ or some other IDE, then you should add both source files to your project and it will do it automatically. If you are building from a command line, then you have to compile both cpp files.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    ok friends.
    Thanks for all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM