-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.

7
  • 2
    Provide a minimal reproducible example. There is no such function function(std::string const&) in your given example. You can try out your example here online. Commented Nov 22, 2022 at 7:08
  • If you are defining (as opposed to declaring) functions in a header file then they must be defined inline. Commented Nov 22, 2022 at 7: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. Commented Nov 22, 2022 at 7:21
  • Please update the question with the header file code relating to the error you quoted. Commented Nov 22, 2022 at 7: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. Commented Nov 22, 2022 at 7:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.