includein cpp file in a cpp file

This is a discussion on includein cpp file in a cpp file within the C++ Programming forums, part of the General Programming Boards category; 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 ...

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

    includein cpp file in a cpp file

    I read this:
    FAQ: Header files, include statements

    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,344
    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
    Portugal
    Posts
    7,581
    This is a thread that may help you understand better how you should structure your files.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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,077
    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 12:00 PM.
    Sent from my iPad®

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> 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, 05:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  4. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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