Multiple definitions of header file

I'm making a text game for my players in DND right now, and I can't figure out why I'm getting the error: multiple definitions of functions.cpp
I'm running it with onlinegdb.com right now, but it works with Ubuntu 18.04

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<iostream>
#include<string>
#include<algorithm>
#include "functions.cpp"


using namespace std;


int main()
{
    printInfo();
}


functions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<string>

using namespace std;

void printInfo()
{
    cout << "Welcome to Ryxaar's Dungeon Delver!  Pick your class:  " << endl;
    cout << "1.  Fighter, Close range jack of all trades\n";
    cout << "2.  Wizard, High damage but low defense, magical expert\n";
    cout << "3.  Ranger, Medium range sustained damage, ranged weapons expert" << endl;
}
The way you have it, it will "work" if you only compile the main file. But if you also try to compile the other (and link it with the first file) you'll get the multiple definition.

The problem is that you don't have a header file. Instead you've just "included" the source file. You're supposed t create a header file for the cpp file that provides the information needed to use the functions/etc. in it and include that in the main file. Then compile the source files separately and link them.

So you need a file called "functions.h":

1
2
3
4
5
6
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

void printInfo();

#endif 

Include it in both the main file and also functions.cpp.
Topic archived. No new replies allowed.