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

标签 c++ class namespaces header-files

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

我的头文件 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();

您应该删除其中一个。

编辑:现在您在 header 末尾缺少一个#endif,而在.cpp 文件...

关于c++ - 在命名空间/头文件中找不到类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10305791/

相关文章:

c++ - 在 bmp 文件中保存屏幕截图的设备上下文

php - class_exists with namespace 不工作 PHP 7.2

java - 为什么你会想要一个没有在其中声明公共(public)类的 Java 文件?

c++ - 在类中实例化类

objective-c - 为什么 Objective-C 没有命名空间?

c++ - 无法在 Windows 下以正常的方式使用 gmp 和 uint64_t

c++ - 我的代码通过了测试用例,但是在我提交时显示了错误的答案

c++ - 最佳实践 : file open/close operations

javascript - 如何调用 javaScript 函数

javascript - Javascript 中的空函数使用