在命名空间/头文件中找不到类

Can't find class in a namespace / header file

本文关键字:找不到 文件 命名空间      更新时间:2023-10-16

我创建了一个类并将其拆分为源文件和头文件,但我无法让它们相互通信。

我的头文件,GridLayout.h看起来像这样:

#ifndef GRIDLAYOUT_H_INCLUDED
#define GRIDLAYOUT_H_INCLUDED
#include <vector>
#include <types.h>
#include "GridPlaceable.h"
namespace Spaceships {
class GridLayout {
    //consider replace with std::list
    typedef std::vector<GridPlaceable*> column;
    public:
        GridLayout();
        ~GridLayout();
        void arrange();
        void splitColumn(size_t colNo, distance position);
        void splitRow(size_t rowNo, distance position);
        bool placeOne(GridPlaceable* thatOne);
private:
        bool tryToFit(GridPlaceable* thatOne, size_t startCol, size_t startCell);
        std::vector<column> wholeGrid;
        std::vector<GridPlaceable*> toPlace;
        std::vector<distance> rowHeights, colWidths;
        std::vector<size_t> firstEmpties;
        bool mandates;
};
};

GridLayout.cpp看起来像:

#include "GridLayout.h"
namespace Spaceships {
GridLayout::GridLayout() {
}
//GridLayout::aBunchOfOtherFunctions() { }
}
#endif

当我编译时,我收到一大堆GridLayout does not name a type错误。可能是什么原因造成的?我似乎记得曾经通过扔一堆分号来解决类似的问题,但这次似乎不起作用。

您的"实际头文件"包含默认构造函数的两个声明:

public:
    GridLayout();
    GridLayout();

您应该删除其中之一。

编辑:现在您在标题末尾缺少一个#endif,并且在.cpp文件的末尾有一个太多......

想通了!我会把它放在这里,以防它对某人有帮助。

我必须将 GridLayout.h 设置为在某个时候编译,然后将其更改回来,因为目录中有一个 GridLayout.gch。因此,编译器必须直接这样做,完全忽略了.h。我删除了该文件,现在它向我显示了代码中的所有真正错误。