• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Importing code from another .cpp file

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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”?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic