Feb 23, 2011 at 9:35am UTC
I've tried to run this code but my compiler said "undefined reference to addition(int, int)
This is the first time I tried to run a program from different cpp files.
In add.h
1 2 3 4
#include <iostream>
using namespace std;
int addition(int a, int b);
In main.cpp
1 2 3 4 5 6 7 8 9
#include "add.h"
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
In addition.cpp
1 2 3 4 5 6 7 8
#include "add.h"
int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}
That code is from Juan Soulie. It runs properly using functions(in one cpp file)
but not in structured programming.\
What is the problem?
Last edited on Feb 23, 2011 at 9:43am UTC
Feb 23, 2011 at 12:48pm UTC
You also include addition.cpp in main.cpp
and no need of writing #include"add.h" in addition.cpp
that should work
Feb 23, 2011 at 1:04pm UTC
@mainframe639
You need to compile both source files and link them in the final executable.
@empress
Never #include source files.
Feb 23, 2011 at 3:26pm UTC
@ Bazzy: how can I link them?
Feb 23, 2011 at 3:29pm UTC
How are you compiling?
If you have an IDE it should do everything for you.
If you are using g++ you simply pass both sources and it will compile and link them
eg: g++ main.cpp addition.cpp -o yourprogram