c++ - #includes 的问题

标签 c++ include

我以为我可以使用#pragma once 来解决我的问题,但它不起作用。

这是我的问题。

我有 Agui.h,我想将它作为我的主要标题,其中包括: 就是这样:

#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 看起来像这样:

#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();
};

还有有问题的,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();
};

编译器没有看到 AguiWidgetBase 的 AguiBaseTypes。这导致

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

我该如何解决这个问题?还有没有一种方法可以像我想要的那样在 Agui.h 随处包含,而不是随着时间的推移会变得困惑的个别包含?

谢谢

最佳答案

(对 Milo 的阐述)

你有相互包含或循环包含,你需要打破这些循环,否则你将永远无法编译代码。

我可以从 AguiWidgetBase.h 中看到它引用了 AguiBaseTypes.h 中的 AguiColor,但是两个 header 都试图包含 Agui.h,而 Agui.h 本身试图包含其他两个。

您应该重新组织 header ,以便它们只有 #include他们想要的。

你应该有一个分层系统:

AguiWidgetBase.h 应该包括:

  • AguiBaseTypes.h

AguiBaseTypes.h 应该包括:

  • <string>

Agui.h 可以包含您喜欢的任何内容,并且可以包含在您的应用程序模块中。

另外,你应该引用Pragma_once有关#pragma once 的更多信息并包含守卫。

关于c++ - #includes 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3885551/

相关文章:

c - 为什么不推荐使用 gets()?

android - 根据条件在 XML 中包含布局

c++ - 在哪里可以找到有关 C++/STL 方法异常保证的信息?

c++ - 如何将入口点过程从 “WinMain”更改为 “main”或任何自定义函数?

c++ - C/C++ 将字符串放入二维数组?

c++ - 多次调用单个测试 - Google 测试

c++ - Eclipse CDT 包括前缀为 'C:'

php - 我怎样才能让 PHP INCLUDE 工作?

c - 如何在 C 中构建#include

C++ 字符串变成指针