Search code examples
c++avr-gcc

Using a classes within multiple namespaces across multiple files in C++


I'm having a problem compiling the following code with avr-g++ (C++ compiler for AVR micro-controllers).

#ifndef SPI_H_
#define SPI_H_


#include "../LIBcpp.hpp"
namespace uC
{
    namespace SPI
    {
        class Device
        {
        private:
            SPI* m_SPI;
            uC::IO::Pin* m_CSPin;
            ChipSelectPolarity m_CSPolarity;

        public:
            Device(SPI& _SPI, uC::IO::Pin& _CSPin, ChipSelectPolarity _CSPolarity);

            void Select();
            void DeSelect();

            void WriteByte(uint8_t _Data);
            uint8_t WriteReadByte(uint8_t _Data);

            void WriteBytes(uint8_t _Data[], uint8_t _DataLength);
            void WriteReadBytes(uint8_t _Data[], uint8_t _ReadBuffer[], uint8_t _DataLength);
        };
    }
}


#endif /* SPI_H_ */

Note that I have defined several enumerations and classes within this file that are used in this class but have not been included to prevent the code from being too long.

I receive the errors

'IO' in namespace 'uC' does not name a type
'uC::IO' has not been declared
 expected ',' or '...' before '&' token

In my project, I have several files that represent specific modules of the project that I am working on. These files are in a sub-directory named Modules. The header file LIBcpp.hpp is in the directory above that. It includes the all of the header files within the Modules sub-directory.

The class Pin is defined within the namespace IO, which is within the namespace uC. This class is defined in a header file named IO.hpp, which is included by LIBcpp.hpp.

What I have tried:

Including the IO.hpp header file in the SPI.hpp header file - resulting in the same errors

I am at a loss as to how to solve this error. If more code or information is required to solve this problem, I will provide it.

Thanks!

This is IO.hpp, as requested:

#ifndef IO_H_
#define IO_H_


#include "../LIBcpp.hpp"

namespace uC
{
    namespace IO
    {
        class Port
        {
                //Contents removed
        };

        class Pin
        {
                //Contents removed
        };
    }
}


#endif /* IO_H_ */

Solution

  • The class "Pin" is defined within the namespace "IO", which is within the namespace "uC". This class is defined in a header file named "IO.hpp", which is included by "LIBcpp.hpp".

    But IO.hpp includes LIBcpp.hpp. You have circular inclusions- this is extremely bad. You must alter your header structure so that there are no circular inclusions.