This question already has answers here:
Why aren't my include guards preventing recursive inclusion and multiple symbol definitions?

(3个答案)


5个月前关闭。




我下面有两个类,“BitmapIterator”类拒绝识别“Bitmap”类的存在,我不知道为什么...
在创建句柄之前,我已经声明了Bitmap类,HBitmap类,并且我正在使用的其他类可以识别HBitmap。
我已经包含了头文件。
我不知道这个BitmapIterator类是怎么回事...它抛出了一大堆“Bitmap不是类或命名空间”和“语法错误:标识符HBitmap”错误。
#pragma once
#include "Color.h"

#include "BitmapIterator.h"
#include <list>

namespace BitmapGraphics
{
    class Bitmap;
    using HBitmap = std::shared_ptr<Bitmap>;

    class Bitmap
    {
    public:
        using ScanLine = std::list<Color>;

    private:
        using ScanLineCollection = std::list<ScanLine>;

    public:
        using ScanLineIterator = ScanLineCollection::const_iterator;
        using PixelIterator = ScanLine::const_iterator;
        Bitmap()=default;

        Bitmap(const Bitmap&) = default;
        Bitmap(Bitmap&&) noexcept = default;

        Bitmap& operator=(const Bitmap&) = default;
        Bitmap& operator=(Bitmap&&) = default;

        ~Bitmap() noexcept = default;


        Bitmap(uint32_t width, uint32_t height, std::istream& sourceStream);

        ScanLineIterator begin() const noexcept { return myScanLines.begin(); }
        ScanLineIterator end() const noexcept { return myScanLines.end(); }

        int getWidth() const noexcept { return myWidth; }
        int getHeight() const noexcept { return myHeight; }
        int getNumberOfPadBytes() const;

        void read(std::istream& sourceStream);
        void write(std::ostream& destinationStream) const;

        HBitmapIterator createIterator(HBitmap hbitmap);

    private:
        unsigned int myWidth{ 0 };
        unsigned int myHeight{ 0 };

        ScanLineCollection myScanLines;
    };
}

#pragma once
#include "IBitmapIterator.h"
#include "Bitmap.h"
#include "binary_ostream_iterator.h"
#include <list>


namespace BitmapGraphics
{
    class BitmapIterator : public IBitmapIterator
    {
    public:
        BitmapIterator() = default;
        BitmapIterator(const BitmapIterator& other) = delete;
        BitmapIterator(BitmapIterator&& other) = delete;
        BitmapIterator& operator =(const BitmapIterator& other) = delete;
        BitmapIterator& operator =(BitmapIterator&& other) = delete;
        ~BitmapIterator() = default;

        BitmapIterator(HBitmap hbitmap);

        void nextScanLine();
        bool isEndOfImage() const;
        void nextPixel();
        bool isEndOfScanLine() const;

        //void addBitmap(HBitmap hbitmap);

        Color getColor() const;

        int getBitmapWidth() const;
        int getBitmapHeight() const;

    private:
        HBitmap myBitmap;
        Bitmap::ScanLineIterator myScanLineIterator;
        Bitmap::PixelIterator myPixelIterator;



    };
}

最佳答案

Bitmap.h包含BitmapIterator.h,其中包含Bitmap.h,然后再进行编码(由于编译指示而被跳过),然后继续使用Bitmap进行定义。或相反。
头文件不应相互包含。
清理您的依赖项。
尝试从位图中删除迭代器的include,然后在正确的 namespace 中直接声明BitmapIterator。那可能会让您入门。

关于c++ - 不是类或 namespace 名称-除外,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63627144/

10-12 23:54