Structured Programming

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
You also include addition.cpp in main.cpp
and no need of writing #include"add.h" in addition.cpp
that should work
@mainframe639
You need to compile both source files and link them in the final executable.

@empress
Never #include source files.
@ Bazzy: how can I link them?
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
Topic archived. No new replies allowed.