| | |
How to call in the external function?
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); }
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.
"You can't build a reputation on what you are going to do."
![]() |
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
Views: 1210 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays assignment beginner binary borland c++ c/c++ calculator char class classes code compile compiler console constructor conversion convert count data delete desktop dll encryption error file forms fstream function functions game givemetehcodez graph homework http iamthwee ifstream input int java lazy lib link linker list loop looping loops map math matrix memory newbie news number objects output pointer pointers problem program programming project python qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio system template templates text tree url variable vc++ video visual visualstudio win32 window windows winsock wordfrequency wxwidgets






