| | |
How to call in the external function?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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
functions.cpp
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)
#include <iostream> using namespace std; int main() { int summation; int a, b; cout <<"Please Enter the two digits to add"<<endl; cin>>a>>b; //add external f(x) from function.cpp extern int addition(a, b); summation = addition(a, b); cout<<"The sum is "<<summation; return 0; }
functions.cpp
C++ Syntax (Toggle Plain Text)
int addition(int a, int b) { int sum; sum = a+b; return(sum); }
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
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Add
...
main.cpp
Now just compile and link them (main.cpp and functions.cpp) togheter ...
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)
#include <iostream> using namespace std; // use the external addition function extern "C++" int addition(int a, int b); int main() { int summation; int a, b; cout <<"Please Enter the two digits to add"<<endl; cin>>a>>b; summation = addition(a, b); cout<<"The sum is "<<summation; return 0; }
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."
![]() |
Similar Threads
- trouble collecting data to external file frorm a call inside a function (C)
- Calling external files (C++)
- Call external program in C++ (Linux) (C++)
- Hooked Procedure Early Return In/External Trigger (C++)
- Need help understanding external functions (C)
- Call a external js function into another js function (JavaScript / DHTML / AJAX)
Other Threads in the C++ Forum
- Previous Thread: Double linked list
- Next Thread: How to put a int name into a messagebox
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






