- #1
Nishiura_high
- 7
- 0
Homework Statement
I can't understand what's causing this bug.
I'm not trying to actually use this program, but it's just something I made up to learn how it works, and understand why things don't work, while I'm learning C++. So my question is, what's causing this program to get this error?
Homework Equations
add.cpp
Code:
int add(int x, int y)
{
return x + y;
}
main.cpp
Code:
#include <iostream>
#include "math.h"
using namespace std;
int main()
{
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
subtract(1, 2);
hello(4, 5);
return 0;
}
subtract.cpp
Code:
#include "math.h"
int subtract(int x, int y)
{
return add(4, 5)
}
math.h
Code:
#ifndef MATH_H
#define MATH_H
int add(int x, int y);
int subtract(int x, int y);
int hello(int x, int y)
{
return 4;
}
#endif
Code:
g++ -g *.cpp
and the output is:
Code:
/home/myname/Desktop/CS/math.h:8: multiple definition of 'hello(int,int)'
/tmp/ccWAqLWY.o:/home/myname/Desktop/CS/math.h:8: first defined here
collect2: ld returned 1 exit status
The Attempt at a Solution
I think it has to do with the header file includes. I thought that it isn't being defined twice, because the second time math.h is included, it would see the #ifndef and not redefine it again. (?)
Can anyone explain what went wrong? Much thanks.