How to call in the external function?

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Thread Solved

Join Date: Jun 2007
Posts: 1,726
Reputation: evstevemd is a jewel in the rough evstevemd is a jewel in the rough evstevemd is a jewel in the rough 
Solved Threads: 153
evstevemd's Avatar
evstevemd evstevemd is offline Offline
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?
---- Python, C++ PHP and Java ----
"Try to be expert in everything and you become expert in nothing" - Me
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2,008
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: 218
tux4life's Avatar
tux4life tux4life is offline Offline
Postaholic

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.
"You can't build a reputation on what you are going to do."
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,726
Reputation: evstevemd is a jewel in the rough evstevemd is a jewel in the rough evstevemd is a jewel in the rough 
Solved Threads: 153
evstevemd's Avatar
evstevemd evstevemd is offline Offline
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?
---- Python, C++ PHP and Java ----
"Try to be expert in everything and you become expert in nothing" - Me
Reply With Quote Quick reply to this message  
Reply

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




Views: 1210 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC