• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Importing code from another .cpp file

 
Bhavesh Sangwan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21007
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create a header file with the declaration (not the definition) of the add function:

Then include that header file in your MyFile_1.cpp:

 
Yashwanth Raj
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<> is for including system header files, "" for including your own header files.

See What is the difference between #include <filename> and #include “filename”?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic