Hi
i wanna execute this program with gcc, but i had this problem:
undefined reference to `ajouteDeux(int)'
collect2: ld returned 1 exit status
math.h:
#ifndef MATH_H_INCLUDED
#define MATH_H_INCLUDED
int ajouteDeux(int nombreRecu);
#endif // MATH_H_INCLUDED
math.cc:
#include "math.h"
int ajouteDeux(int nombreRecu)
{
int valeur(nombreRecu + 2);
return valeur;
}
main.cc:
#include <iostream>
#include "math.h"
using namespace std;
int main(void)
{
int a(2),b(2);
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;
b = ajouteDeux(a); //Appel de la fonction
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;
int ajouteDeux(int nombreRecu)
{
int valeur(nombreRecu + 2);
return valeur;
}
main.cc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include "math.h"
usingnamespace std;
int main(void)
{
int a(2),b(2);
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;
b = ajouteDeux(a); //Appel de la fonction
cout << "Valeur de a : " << a << endl;
cout << "Valeur de b : " << b << endl;
return 0;
}
@bluecoder - That is also a solution , but it 1) changes the original code structure/organization which the original question posted(Not that it is a big project or something, but more of a test study code) 2) Putting function definitions in header file, although ok, but not a good thing. Because if u change the function in the header file, every source file which includes this header, needs to be recompiled after that, which could be time consuming and unncessary if that source includes the header not for the function but for some other data(structure , class ) from this header file.