-5

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 off2(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))

  • 4
    Why are you `#include`-ing .cpp files!? – Borgleader Apr 16 '15 at 14:07
  • 2
    Never (never never never never....) #include a source file without a damned good reason (which rarely exists). If such a reason does exist, never include it in more than one source file. – mah Apr 16 '15 at 14:11
  • The code you show can't possibly produce the error you cite. This code doesn't define `f2` even once, let alone provide multiple definitions. – Igor Tandetnik Apr 16 '15 at 14:23
  • Ok, I delete #include "func2.cpp" from head.h, and it works ok. But when I delete #include "func1.cpp" from header, there is another error - "undefined reference to". So, what's wrong and how to do it right??? – Евгений Чередниченко Apr 16 '15 at 14:26
  • Please show me the right code! Thank you! – Евгений Чередниченко Apr 16 '15 at 14:34
  • @ЕвгенийЧередниченко _"Please show me the right code!"_ Please learn the basics first. Regarding undefined reference errors, [see here](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) please, how to fix these. – πάντα ῥεῖ Apr 16 '15 at 14:39

1 Answers1

0

Try this:

head.h

#ifndef HEAD_INCLUDED
#define HEAD_INCLUDED

int f1(int);
int f2(int);

#endif // HEAD_INCLUDE

head.cpp

#include "head.h"

int f1(int a)
{
    return a+a;
}

int f2(int a)
{
    return a+a;
}

test1.cpp

#include <iostream>
#include "head.h"

using namespace std;


int main(){

    int n = 5;

    int val1 = f1(3);
    int val2 = f2(5);

    cout << "val1 = " << val1 << endl;
    cout << "val2 = " << val2 << endl;
    return 0;
}
  1. compile and execute commands in linux g++ -o test1.o head.cpp test1.cpp ./test1.o
ivan.sim
  • 8,972
  • 8
  • 47
  • 63
Nancy Le
  • 1
  • 1