gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:25:57 27 4
gpt4 key购买 nike

我在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com