943,075 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2894
  • C++ RSS
Apr 24th, 2009
0

How to call in the external function?

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. int addition(int a, int b)
  2. {
  3. int sum;
  4. sum = a+b;
  5. return(sum);
  6. }
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Apr 24th, 2009
0

Re: How to call in the external function?

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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 24th, 2009
0

Re: How to call in the external function?

Great man! I appreciate!
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Double linked list
Next Thread in C++ Forum Timeline: How to put a int name into a messagebox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC