-1

I have a project consisting of 6 files; main.cpp, functions.h, tennisplayer.h, tennisplayer.cpp, tennisteam.h & tennisteam.cpp which are roughly defined as follows:

// main.cpp

#include "tennisteam.h"
#include <iostream>
#include <string>
#include <exception>

// some main() code that needs functions.h definitions
// functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include "tennisplayer.h"
#include <iostream>
#include <string>
#include <limits>

// some constants & function definitions needed by both main.cpp & tennisteam.cpp

#endif
// tennisplayer.h

#ifndef TENNISPLAYER_H
#define TENNISPLAYER_H

#include <string>
#include <vector>

// tennisplayer class declarations

#endif
// tennisplayer.cpp

#include "tennisplayer.h"
#include <iostream>
#include <fstream>

// tennisplayer class definitions
// tennisteam.h

#ifndef TENNISTEAM_H
#define TENNISTEAM_H

#include "tennisplayer.h"
#include <string>
#include <vector>

// 

#endif
// tennisteam.cpp

#include "tennisteam.h"
#include <iostream>
#include <fstream>

// tennisteam class definitions

However, when I include functions.h into both main.cpp & tennisteam.cpp via tennisteam.h I get a linker error along the lines of:

/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccv30cX0.o:tennisteam.cpp:(.text+0x0): multiple definition of `function(std::string const&)'; /tmp/ccRThgpp.o:main.cpp:(.text+0x0): first defined here

I'm aware this is a linker error. I've looked around for a fix but all I come across are posts instructing me to use include guards which I have done already. Is there something I'm missing here? Any help would be appreciated.

xvymnp
  • 21
  • 1
  • 2
    Provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). There is no such function `function(std::string const&)` in your given example. You can try out your example [here online](https://onlinegdb.com/d2wMwTg8I). – Jason Nov 22 '22 at 07:08
  • If you are **defining** (as opposed to declaring) functions in a header file then they must be defined `inline`. – john Nov 22 '22 at 07:17
  • I reopened this because the although the suggested duplicate gives a good explanation of include guards, that is not the problem here (as the OP knows). Clearly the problem here is with the code in the header file. Unfortunately the OP has given little indication of what that is. So maybe this question could be closed for lack of debugging details. – john Nov 22 '22 at 07:21
  • Please update the question with the header file code relating to the error you quoted. – john Nov 22 '22 at 07:22
  • The code in a question should be a minimal amount needed to demonstrate the issue. So -- *"I have a project consisting of 6 files"* -- Should I infer from this that a project consisting of only 5 files is too small to reproduce the issue? You cannot reduce your demonstration/example to the four files `functions.h`, `main.cpp`, `tennisteam.cpp`, and `tennisteam.h`, plus another file? Not down to just `functions.h`, `main.cpp` and `tennisteam.cpp`? The simpler your example, the easier your question is to answer, and the more likely that your question will help someone else in the cuture. – JaMiT Nov 22 '22 at 07:28
  • The relevant information to answer this question is provided: The linker error. Reading that (despite its unfortunate formatting) is enough. – bitmask Nov 22 '22 at 07:31
  • The problem is probably in `// some constants & function definitions needed by both main.cpp & tennisteam.cpp`. But you haven't shown any of that code, so any attempt to answer would just be guessing. When you don't understand what's going on, summarizing is usually a mistake. – Pete Becker Nov 22 '22 at 15:21

1 Answers1

1

You have function function(std::string const&) that you not only declared but also defined in your header file. If you need to have it defined there instead of a .cpp file, mark it as inline.

This results in two cpp files (namely main.cpp and tennisteam.cpp) ending up with a definition of that function, because they both include that header file.

bitmask
  • 32,434
  • 14
  • 99
  • 159