Meaningless Drivel is fun!*
The moose likes C / C++ and the fly likes Importing code from another .cpp file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Importing code from another .cpp file" Watch "Importing code from another .cpp file" New topic
Author

Importing code from another .cpp file

Bhavesh Sangwan
Ranch Hand

Joined: Aug 08, 2012
Posts: 57
Hi All...!

I'm using Eclipse IDE and MinGW compiler and have added two .cpp files in my C++ project.

One file having main() method and snother without main() - only one method - display(int, int).

Now I want use that display() function written in MyFile_2.cpp into main() function of MFile_1.cpp file. So please help me how to use some code from another .cpp file.

My files are listed below...

MyFile_2.cpp


MyFile_1.cpp



Please correct me...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Bhavesh Sangwan wrote:Hi All...!

I'm using Eclipse IDE and MinGW compiler and have added two .cpp files in my C++ project.

One file having main() method and snother without main() - only one method - display(int, int).

Now I want use that display() function written in MyFile_2.cpp into main() function of MFile_1.cpp file. So please help me how to use some code from another .cpp file.

Please correct me...



Isn't that what the include files are for? You will need to have an include file that defines the display function, and include that file into your other c++ source file.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Create a header file with the declaration (not the definition) of the add function:

Then include that header file in your MyFile_1.cpp:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Yashwanth Raj
Greenhorn

Joined: Jan 18, 2013
Posts: 18
sir i have a doubt.you have used <>for including iostream and "" for including a user defined header.what are their difference
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

<> is for including system header files, "" for including your own header files.

See What is the difference between #include <filename> and #include “filename”?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Importing code from another .cpp file
 
Similar Threads
Exhaustive Search/Brute Force Algorithm for 0-1 Knapsack Problem
beginner problem
Help with c++ and cmd.exe
Divisibity Test Question
c++ without fear HELP