tags:

views:

82

answers:

2

I thought I could use #pragma once to solve my problems but it's not working.

Here is my issue.

I have Agui.h which I wanted to be my main header which everything includes: this is it:

#pragma once

/*
    This header includes all the required headers
    required for Agui

      Author: Josh
*/

//Standard library (STL)

#include <stdlib.h>  
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>

//C runtime
#include <cmath> 
#include <ctime>

//Allegro 5 
#include <allegro5/allegro.h>
#include <allegro5/allegro5.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>

#include "AguiBaseTypes.h"
#include "AguiWidgetBase.h"

AguiBaseTypes.h looks like this:

#pragma once
#include "Agui.h"
#define AguiBitmap ALLEGRO_BITMAP

/*
   This header contains classes for basic types.
   These include:
   Point,
   Size, 
   Rectangle,
   Color
   and their floating equivalents

      Author: Josh
*/


//integer Point class
class AguiPoint {
    int x;
    int y;
public:
    int getX();
    int getY();
    void setX(int x);
    void setY(int y);
    void set(int x, int y);
    AguiPoint(int x, int y);
    AguiPoint();
    std::string toString();
    std::string xToString();
    std::string yToString();

};

//floating version of Agui Point
class AguiPointf {
    float x;
    float y;
public:
    float getX();
    float getY();
    void setX(float x);
    void setY(float y);
    void set(float x, float y);
    AguiPointf(float x, float y);
    AguiPointf(AguiPoint p);
    AguiPointf();
    std::string toString();
    std::string xToString();
    std::string yToString();
};

//Integer rectangle class
class AguiRectangle {
    int x;
    int y;
    int width;
    int height;
public:

    bool isEmpty();
    int getTop();
    int getLeft();
    int getBottom();
    int getRight();
    AguiPoint getTopLeft();
    AguiPoint getBottomRight();
};

class AguiColor {
    unsigned char r;
    unsigned char g;
    unsigned char b;
    unsigned char a;
void verifyColorBounds();
public:
    AguiColor(int r, int g, int b, int a);
    AguiColor(float r, float g, float b, float a);
    AguiColor();
    int getR();
    int getG();
    int getB();
    int getA();
};

and the problematic one, AguiWidgetBase.h

#pragma once
#include "Agui.h"



/*
    This is the base class for all widgets

      Author: Josh
*/
class AguiWidgetBase
{
    //variables
    AguiColor tintColor;
    AguiColor fontColor;
    //private methods
    void zeroMemory();
    virtual void onPaint();
    virtual void onTintColorChanged(AguiColor color);
    void (*onPaintCallback)(AguiRectangle clientRect);
    void (*onTintColorChangedCallback)();


public:
    AguiWidgetBase(void);
    ~AguiWidgetBase(void);
    void paint();
    void setTintColor(AguiColor color);
    AguiColor getBackColor();
};

The compiler is not seeing AguiBaseTypes for AguiWidgetBase. This causes

Warning 13  warning C4183: 'getBackColor': missing return type; assumed to be a member function returning 'int' c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  29
Error   2   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  14
Error   3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  14
Error   5   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  15
Error   6   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  15
Error   11  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  29
Error   12  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  29
Error   1   error C2146: syntax error : missing ';' before identifier 'tintColor'   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  14
Error   10  error C2146: syntax error : missing ';' before identifier 'getBackColor'    c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  29
Error   4   error C2146: syntax error : missing ';' before identifier 'fontColor'   c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  15
Error   8   error C2061: syntax error : identifier 'AguiRectangle'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  20
Error   7   error C2061: syntax error : identifier 'AguiColor'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  19
Error   9   error C2061: syntax error : identifier 'AguiColor'  c:\users\josh\documents\visual studio 2008\projects\agui\alleg_5\agui\AguiWidgetBase.h  28

How could I resolve this? Also is there a way to do it like I want where Agui.h is included everywhere, instead of individual includes which can get confusing over time?

Thanks

+3  A: 

(Elaboration for Milo)

You've got mutually inclusive or cyclic includes and you need to break these cycles or you will never get the code to compile.

I can see from AguiWidgetBase.h that it refers to AguiColor from AguiBaseTypes.h, but both headers try to include Agui.h, and Agui.h itself tries to include both of the others.

You should re-organise the headers so that they only #include the ones they want.

You should have a hierarchical system so:

AguiWidgetBase.h should include:

  • AguiBaseTypes.h

AguiBaseTypes.h should include:

  • <string>

Agui.h can include anything you like and can be included by your application modules.

Also, you should refer to Pragma_once for more information about #pragma once and include guards.

quamrana
I'm not sure I fully understand, could you elaborate?
Milo
Which bits don't you understand?
quamrana
+2  A: 

#pragma once is not guaranteed to be supported on all compilers, use include guards instead. Furthermore, you have cyclic includes: "Agui.h" includes "AguiBaseTypes.h" and vice versa. That's not the way it's meant to be.

A global include file might be okay to reduce boilerplate code in source files, but in header files, you should include exactly the necessary headers, or else you get the problem that you described.

AndiDog
#pragma once are fine on most compilers. They're supported properly on Visual Studio, GCC, CLang and on the console compilers I use. Although AndiDog is correct in saying that they are not guaranteed to be supported, in practice they are on all the popular modern compilers.
Cthutu