0

Why I can't use my custom header file? The IDE marks the include as unused. In C++ I have very mediocre knowledge. Perhaps I misunderstand something and incorrectly implement the .h and .cpp files.

There is a header file describing the structure of the mgportfolio.h class:

class LIBMGPORTFOLIO_EXPORT MgPortfolio : IComponent
    {
    public:
        MgPortfolio();
        explicit MgPortfolio();
        ~MgPortfolio();
        
        void accept(IVisitor& visitor) override;

    private: 
        //std::string m_portfolioFilePath;
        //std::string m_portfolioName;
        //std::string m_owner;
    };

Next, I want to implement a pattern "visitor" for some of my logic. I add one more header file visitor.h:

#pragma once
#include "portfoliooption.h"
#include "playlistitem.h"
#include "archiveddata.h"
#include "mgportfolio.h" //Ide underlines this header file as unused. Although in the first visit() method I use the object from this file.

namespace mg
{
    class IVisitor
    {
    public:
        virtual ~IVisitor() = default;

        virtual void visit(MgPortfolio& portfolio) const; //here the ide suggests including #include "mgportfolio.h" but I already did that above.
        virtual void visit(PortfolioOption& portfolioOption) const; //here and below the header file is connected without problems
        virtual void visit(PlaylistItem& playlistItem) const;
        virtual void visit(ArchivedData& archivedData) const;
    };

    class IComponent 
    {
    public:
        virtual ~IComponent() = default;
        virtual void accept(IVisitor& visitor) = 0;
    };
}

The PortfolioOption, PlaylistItem, ArchivedData classes differ from MgPortfolio only in that they do not have an implementation in .cpp files. As soon as I add the implementation, the same error appears as with MgPortfolio.What could be my problem?

  • 3
    Do you also have `visitor.h` included in `mgportfolio.h`? – Yksisarvinen Feb 07 '22 at 15:02
  • 3
    Make sure your headers don't form a loop of includes. – drescherjm Feb 07 '22 at 15:02
  • 2
    In `visitor.h` you should not include `mgportfolio.h` that would cause this: [https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) To fix `visitor.h` should not include any of these files. Instead it should use forward declarations. – drescherjm Feb 07 '22 at 15:05

1 Answers1

2

Why can't I include my header file in another file?

You can include a file in another.

The IDE marks the include as unused.

The tool seems to be warning you that you are including a file unnecessarily. You should avoid doing that.

Although in the first visit() method I use the object from this file.

You don't need to include the definition of MgPortfolio to do that. A declaration is sufficient:

class MgPortfolio;

the same error appears

"Is unused" is generally not an error, but a warning.

eerorika
  • 232,697
  • 12
  • 197
  • 326