Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've started a C++ project, and for the first time I'm trying to write in OOP style.

Before this, my programming style was just to cut-and-paste functions from the .cpp files out into .h files. This way I kept common code in header files.

However now I'm trying to do OOP style programming, while reusing my "helper" .h files. Those files have very little code, for example some interpolation or number formatting.

Here is where I'm confused. I've gone quite far, but now I'm in a point where I'd like to include a header-only helper function file I've made before. The problem is that if I include it in a class, it works, but then I cannot include it in another class.

The error message I receive is

error LNK2005: "float __cdecl calculateZ(int)" (?calculateZ@@YAMH@Z) already defined in Fruit.obj

I've tried putting pragma once before all header files but it didn't help. But why did it happen only for my header-only file, while lot of other header files are included more then once?

Other than solving this problem what do you recommend as a general OOP style programming for header files? For small projects with a few classes, wouldn't it be better to include everything in a common header file and just include a "common.h" in all class? If yes, could you recommend me a solution what would simply put all required includes in one header file? I mean one #include "common.h" on top of each other file. Is this method bad?

I'm asking this question because I find that I spend way more time figuring out what include goes before what header files. Before, when I wasn't doing OOP style programming these things have never existed. Where can I learn how to manage these includes in a clean way?

Here is the code for the project (bodies deleted):

simulatorApp.cpp

#include "cinder/app/AppBasic.h"
#include "../include/FruitManager.h"

#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Vector.h"
#include "cinder/ImageIO.h"
#include "cinder/Utilities.h"
#include "cinder/params/Params.h"
#include "cinder/Camera.h"
#include "cinder/Rand.h"

//#include "../include/helper.h" // <- including this breakes the code, as it's already included below

#include "../include/showWin32Console.h"

using namespace ci;
using namespace ci::app;
using namespace std;

... function body

FruitManager.cpp

#include "../include/FruitManager.h"

#include "cinder/Vector.h"
#include <boost/algorithm/string.hpp>
... function body

Fruit.cpp

#include "../include/Fruit.h"
#include "../include/helper.h" // <- included here first

#include "cinder/Vector.h"
#include <boost/algorithm/string.hpp>
... function body

FruitManager.h

#pragma once
#include "cinder/app/AppBasic.h"
#include "Fruit.h"

#include <list>

using namespace ci;
using namespace ci::app;
using namespace std;

class FruitManager
{
    public:
        FruitManager();

        void readFruitDimensions();
        void readFruitScripLine( string line );
        void addFruitsFromScriptFile( string scriptfile );
        void draw();
        struct boundingBox
        {
            Vec3f subvolmin;
            Vec3f subvolmax;
        };

        map<string, boundingBox> FruitDimensions;
        list<Fruit> FruitList;
};

Fruit.h

#pragma once
#include "cinder/app/AppBasic.h"

using namespace ci;
using namespace ci::app;
using namespace std;

class Fruit
{
    public:
        Fruit();
        void draw();

        string fruit_name;
        Vec3f subvolmin;
        Vec3f subvolmax;
        Vec2f positionXY;
};

showWin32Console.h

#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <string>

void showWin32Console()
{
... function body
}

helper.h (problematic)

#pragma once

string numformat( double num, int left, int right )
{
... function body
}

float quadraticBezier( float x, float a, float b )
{
... function body
}

float scaleFun( float f, float f0, float f1 )
{
... function body
}

float calculateZ( int framenumber )
{
... function body
}
share|improve this question

2 Answers

up vote 5 down vote accepted

Header files are for declaration (NOT definition).

Usually you don't want to put function definition in a header file. If you do then you MUST prefix it with inline. The inline keyword is a marker for linker that it should expect multiple copies of the function (one in each translation unit) and it should either coalesces them into a single version or ignore the problem (implementation detail).

You should also protect yourself from multiple inclusion. There are two options here:

  1. include guards
  2. pragma once

Though lots of compilers do support pragma once it is not universally supported. So to be safe you should use include guards.

Inside a public header file; Never

 using namespace X;

This will pollute the namespace for anybody that includes your header file. This means a lot of people will refuse to use your code as it will break stuff. So just don't do it.

Its not so bad in a source file. But for any project larger than a toy project you would not want to do it much. Prefer to be explicit by prefixing your objects in type with NS:: (where NS is your namespace). If you must do it then rather than include the whole namespace only do it for specific types that you need.

 using std::cout;  // Now you can do cout << "Plop\n";
 using std::cin;
 // etc

Inside a public header file; Prefer

  // to use forward declaration rather than include.
  // You only need to include the header file for TypeX
  // iff
  //    a: There is a member of type TypeX
  //    b: A class inherits from TypeX
  //    c: You pass a parameter of type TypeX to a function by value (rare)
  //
  // In all other situations you should forward declare TypeX

  class TypeX; // That's it.

For general advice on how to split code see: http://stackoverflow.com/q/280033/14065

share|improve this answer
Thanks for the clearing it up! About pragma once (if I go this way): should I include it before every header file? Even before those ones with inline functions? About header-only: why would it be bad for a simple prototyping work? Also, how does boost work without inline functions? I found lot of boost libs are header only but the only do include guards. – zsero Jul 20 '12 at 19:04
Every public header file should use protection from multiple includes. Private header you have some flexibility but usually it is just convenient to always put the protection in. I personally always use header guards and avoid pragma once – Loki Astari Jul 20 '12 at 19:07
Header only libraries all the functions need to be prefixed with inline. Note: inline keyword has nothing to do with inline the code. – Loki Astari Jul 20 '12 at 19:09
Thanks for the this as well as for the detailed answer! I have a lot to read in the linked threads! Thanks! – zsero Jul 20 '12 at 19:09

Though #pragma once can avoid multiple inclusion of header file and supported by many compilers, it's non standard. You can use #include guards instead.

I would suggest you to go through this google coding guideline link http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Header_Files

share|improve this answer
1  
Guggle style guide is awful for general C++ development. It is only useful if you work at goggle. It contains lots of terrible advice for general C++ (but good for googles code base). – Loki Astari Jul 20 '12 at 18:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.