How to call in the external function?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2007
Posts: 1,301
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 124
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

How to call in the external function?

 
0
  #1
Apr 24th, 2009
I have two files and I would like to call addition function from functions.cpp in main.cpp. But I get error:

In function `int main()':
error: `addition' has both `extern' and initializer
error: initializer expression list treated as compound expression
warning: left-hand operand of comma has no effect
error: `addition' cannot be used as a function|
||=== Build finished: 3 errors, 1 warnings ===|


Here are files
main.cpp
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int summation;
  8. int a, b;
  9. cout <<"Please Enter the two digits to add"<<endl;
  10. cin>>a>>b;
  11. //add external f(x) from function.cpp
  12. extern int addition(a, b);
  13. summation = addition(a, b);
  14. cout<<"The sum is "<<summation;
  15. return 0;
  16.  
  17. }

functions.cpp
  1. int addition(int a, int b)
  2. {
  3. int sum;
  4. sum = a+b;
  5. return(sum);
  6. }
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: How to call in the external function?

 
0
  #2
Apr 24th, 2009
Add extern "C++" int addition(int a, int b); to main.cpp (outside your main function), delete the instruction external int addition(a, b); from inside your main function ...

main.cpp
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // use the external addition function
  6. extern "C++" int addition(int a, int b);
  7.  
  8. int main()
  9. {
  10. int summation;
  11. int a, b;
  12. cout <<"Please Enter the two digits to add"<<endl;
  13. cin>>a>>b;
  14. summation = addition(a, b);
  15. cout<<"The sum is "<<summation;
  16. return 0;
  17.  
  18. }
Now just compile and link them (main.cpp and functions.cpp) togheter ...
Last edited by tux4life; Apr 24th, 2009 at 5:20 pm.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,301
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 124
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: How to call in the external function?

 
0
  #3
Apr 24th, 2009
Great man! I appreciate!
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC