Yes, I happened to guess correctly; your issue is that you are defining a non-inline function (Say) inside of your header file, which is #included into two different compilation units (other.cpp and main.cpp).
You should either add the
inline keyword to your function definition, or only
declare the function in the header file, and implement (define) it in either main.cpp or other.cpp.
e.g.
header.h
1 2 3 4 5 6 7 8 9
|
#ifndef HEADER_H
#define HEADER_H
#include <string>
// declaration (used by multiple files)
void Say(string x);
#endif
|
other.cpp
1 2 3 4 5 6 7
|
#include "header.h"
// definition (only compiled once)
void Say(string x)
{
// ...
}
|
main.cpp
1 2 3 4 5 6
|
#include "header.h"
int main()
{
Say("blah");
}
|
______________________________
When you "#include" something, think of it as directly copying the text from the header into the .cpp file. So what happens is you have two .cpp files, x.cpp and y.cpp, and they are both
defining the Say(string) function independently, which causes the "multiple definition" error.
Each compilation unit is built separately in C++, and then they are linked together.