c++ - 在 C++ 中跨多个文件使用多个命名空间中的类

标签 c++ avr-gcc

我在使用 avr-g++(AVR 微 Controller 的 C++ 编译器)编译以下代码时遇到问题。

#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_ */

请注意,我在该文件中定义了几个枚举和类,这些枚举和类在该类中使用,但为了防止代码太长而没有包含在内。

我收到错误

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

在我的项目中,我有几个文件代表我正在处理的项目的特定模块。这些文件位于名为 Modules 的子目录中。头文件 LIBcpp.hpp 位于其上方的目录中。它包含 Modules 子目录中的所有头文件。

Pin 类在命名空间 IO 中定义,它位于命名空间 uC 中。此类在名为 IO.hpp 的头文件中定义,该文件包含在 LIBcpp.hpp 中。

我尝试过的:

SPI.hpp 头文件中包含IO.hpp 头文件——导致同样的错误

我不知道如何解决这个错误。如果需要更多代码或信息来解决这个问题,我会提供。

谢谢!

根据要求,这是 IO.hpp:

#ifndef IO_H_
#define IO_H_


#include "../LIBcpp.hpp"

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

        class Pin
        {
                //Contents removed
        };
    }
}


#endif /* IO_H_ */

最佳答案

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".

但是 IO.hpp 包括 LIBcpp.hpp。你有圆形夹杂物——这是非常糟糕的。您必须更改您的 header 结构,以便没有循环包含。

关于c++ - 在 C++ 中跨多个文件使用多个命名空间中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11822710/

相关文章:

c - 建议 : trying to recognize when a device is not connected

optimization - 为什么这段代码是由 avr-gcc 生成的,它是如何工作的?

c++ - 未检测到空指针

c++ - 在不同的派生类中继承具有不同参数的纯虚方法

c# - 将结构数组从 C++(COM) 传递到 C#

C++语法问题

AVR-GCC 中 PROGMEM 中干净整洁的字符串表

c++ - 如何seccomp一个子进程?

c++ - 如何使用 avr-gcc 在 C/C++ 中执行预主初始化?

c - 在嵌入式系统中存储大整数/值