.h or .cpp

I have the code below and cannot decide if to put it in a .h file or in a .cpp file

#ifndef VERBOSE_H
#define VERBOSE_H

#include "sstring.h"

enum Verbose { none = 0, low = 1, medium = 2, high = 3, diag = 4 };

bool  GetVerbose(String&  S, Verbose&  V) {
  S.ToUpper();
  if (S == String("NONE")) { V = none; return (true); }
  if (S == String("LOW")) { V = low; return (true); }
  if (S == String("MEDIUM")) { V = medium; return (true); }
  if (S == String("HIGH")) { V = high; return (true); }
  if (S == String("DIAG")) { V = diag; return (true); }
  return (false);
}

#endif

That will cause duplicate symbol errors if you include that file in multiple .cpp files because you'd declare the same function body over and over.

I'd change it to this:

#ifndef VERBOSE_H
#define VERBOSE_H

#include "sstring.h"

enum Verbose { none = 0, low = 1, medium = 2, high = 3, diag = 4 };

extern bool  GetVerbose(String&  S, Verbose&  V);
#endif

Put the function body in one and only one .cpp file.

Alternatively, you could make it an inline function, which would prevent it from having a symbol definition at all.

Thank you. If I include it inline, I would not need to have a .cpp file then. Is that right? Is it ok not to have a .cpp on some occasions like this one?

---------- Post updated at 05:02 AM ---------- Previous update was at 04:45 AM ----------

I had originally some code concerning a class (definition and implementation) in a header file put have now split it to have the definition and inline functions in the .h file, and the rest of the implementation in a .ccp file.

This is what I have for the .cpp file

The #ifndef, #define, #include and #endif were in the .h file. Should I remove them from the .cpp file as well, as to your explanation?

#ifndef COMPLEX_H
#define COMPLEX_H

#include <assert.h>
#include <cmath>
#include <iostream>

#include "gendef.h"

//////////////////////////////////////////////////////////////////////////

Complex&  Complex:operator /= (const Complex&  C) {
  assert((C.Re != 0) || (C.Im != 0));
  REAL  mod = (C.Re * C.Re) + (C.Im * C.Im);
  REAL  nr, ni;
  nr = ( (Re * C.Re) + (Im * C.Im) ) / mod;
  ni = ( (C.Re * Im) - (Re * C.Im) ) / mod;
  Re = nr;
  Im = ni;
  return (*this);
}

#endif

Those #ifndef/#define/#endif don't belong there, no. That arrangement is to prevent a .h file from being included more than once -- if you accidentally do so, FILE_H will already be defined and the preprocessor will harmlessly skip over the contents.

The string is completely arbitrary, by the way. You could have #ifndef SLARTIBARTFAST and it would work, as long as SLARTIBARTFAST is only used for one .h file.