I have a "multiple definition of function" error on f2 function. The same error is in CodeBlocks and MS Visual Studio 2008. What changes need for this code to work without errors?
Here is the simple example of my program? that has error:
**main.cpp**
#include <iostream>
#include"head.h"
using namespace std;
int main()
{
int n = 5;
cout<<f1(n)<<endl;
cout<<f2(n)<<endl;
return 0;
}
**head.h**
#ifndef HEAD_INCLUDED
#define HEAD_INCLUDED
#include "func1.cpp"
#include "func2.cpp"
int f1(int);
int f2(int);
#endif // HEAD_INCLUDED
**func1.cpp**
#include <iostream>
using namespace std;
int f1(int a)
{
return a+a;
}
**func2.cpp**
#include <iostream>
using namespace std;
int f1(int a)
{
return a+a;
}
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------
g++ -Wall -fexceptions -g -c /home/laptop/Documents/CodeBlocksProjects/test/func2.cpp -o obj/Debug/func2.o
g++ -Wall -fexceptions -g -c /home/laptop/Documents/CodeBlocksProjects/test/main.cpp -o obj/Debug/main.o
g++ -o bin/Debug/test obj/Debug/func2.o obj/Debug/main.o
obj/Debug/main.o: In function f2(int)':
/home/laptop/Documents/CodeBlocksProjects/test/func2.cpp:5: multiple definition of
f2(int)'
obj/Debug/func2.o:/home/laptop/Documents/CodeBlocksProjects/test/func2.cpp:5: first defined here
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
2 error(s), 0 warning(s) (0 minute(s), 1 second(s))