1

Me and partner are working in a project together and the online compiler we are using is stumping us. We have two files that make up the project

Koffiebord.h The error we are getting is in the first file.

koffiebord.h:7:6: error: redefinition of ‘char leesOptie()’
    7 | char leesOptie() {
      |      ^~~~~~~~~
In file included from koffiebord.cc:4:
koffiebord.h:7:6: note: ‘char leesOptie()’ previously defined here
    7 | char leesOptie() {
      |      ^~~~~~~~~
In file included from koffiebord.cc:47:
koffiebord.h:15:5: error: redefinition of ‘int leesGetal(int)’
   15 | int leesGetal(int maxi) {
      |     ^~~~~~~~~
In file included from koffiebord.cc:4:
//headerfile met classes + leesgetal
#include <iostream>
#include <cstdlib>

using namespace std;

char leesOptie() { 
    a function to read choices
} //leesOptie

int leesGetal(int maxi) { 
    a function to read numbers
} //leesGetal

class vakje {
  not importan
};//bordvakje

class koffiebord {
  not important   
};//koffiebord

the second files important bits (It is called koffiebord.cc)

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "koffiebord.h"
using namespace std;

if you can help that would be much appreciated

tank boi
  • 11
  • 1
  • 1
    Because you are defining functions in a header file, without include guards. – ChrisMM Dec 20 '21 at 16:13
  • 1
    It's time to learn about [header guards](https://stackoverflow.com/questions/2979384/purpose-of-header-guards) and [inline functions](https://stackoverflow.com/questions/5971736/c-inline-function). Good C++ programming requires a good book to introduce these concepts. Guessing what to type and seeing if it compiles will lead to lots of problems in the future. – Drew Dormann Dec 20 '21 at 16:13
  • 1
    You seem to have `#include "koffiebord.h"` on both line 4 and 47 of the source file `koffiebord.cc`. Why? – Some programmer dude Dec 20 '21 at 16:16
  • A lot of compilers also support #pragma once in header files (a bit more convenient). Oh and do try to unlearn "using namespace std" your future you will thank you :) – Pepijn Kramer Dec 20 '21 at 16:16
  • And why do you *define* (implement) global non-member function in a header file? Should any of the functions be *member* function in a class? – Some programmer dude Dec 20 '21 at 16:17
  • 1
    Typically you only declare functions in a header. That way they are not defined over multiple translation units. Inline is an exception to this and basically tells the linker that it will have multiple definitions, but they are all the same. Template definitions (unless fully specialized) also need to go into header files. – floomby Dec 20 '21 at 16:22

0 Answers0