-1

我对以下代码有疑问(这些只是片段,不是完整文件):

菜单.h

#ifndef MENU_H
#define MENU_H

#include <Adafruit_SSD1306.h>
#include <stdint.h>

#include <Config.h>

class MenuHeader; // forward declaration of class MenuHeader
class MenuPage; // forward declaration of class MenuPage

class Menu {
    MenuHeader* m_header{nullptr};
    MenuPage* m_pages[16]{}; // support up to 16 pages
    uint8_t m_pagesCount{0};
    uint8_t m_currentPage{0};

    public:
    void setHeader(MenuHeader* header);
    void addPage(MenuPage* page);
    void goToPage(const char* pageName);
    void next();
    void prev();
    void click();
    void draw(Adafruit_SSD1306* display);
};

#endif

菜单.cpp

#include <Menu.h>
#include <MenuHeader.h>
#include <MenuPage.h>

/* Some other definitions */

void Menu::draw(Adafruit_SSD1306* display) {
    if(m_header != nullptr) {
        display->setCursor(0, HEADER_HEIGHT - 1 + SCREEN_Y_OFFSET);
        m_pages[m_currentPage]->draw(display);
        m_header->draw(display);
    } else {
        display->setCursor(0, SCREEN_Y_OFFSET);
        m_pages[m_currentPage]->draw(display);
    }
}

配置文件

#ifndef CONFIG_H
#define CONFIG_H

#include <stdint.h>

constexpr uint8_t ENCODER_PIN_A = 14; // pin A of rotary encoder
constexpr uint8_t ENCODER_PIN_B = 12; // pin B of rotary encoder
constexpr uint8_t BUTTON_PIN = 13; // pin to which button is connected

constexpr uint8_t SCREEN_WIDTH = 128; // width of screen in pixels
constexpr uint8_t SCREEN_HEIGHT = 64; // height of screen in pixels
constexpr uint8_t CHARS_PER_LINE = 18; // how many characters fit in one line
constexpr uint8_t CHAR_WIDTH = SCREEN_WIDTH/CHARS_PER_LINE; // width of single character
constexpr uint8_t CHAR_HEIGHT = 10; // height of single char
constexpr uint8_t SCREEN_Y_OFFSET = CHAR_HEIGHT; // screen offset, if not using custom font set to 0

constexpr uint8_t HEADER_HEIGHT = 14; // menu header height in pixels

constexpr int8_t TIMEZONE = -1; // timezone

/* Some other not needed stuff */

#endif

所有标题(Menu.h、MenuHeader.h、MenuPage.h)都包含 Config.h。

好吧,编译器似乎不喜欢它。它抛出:

'HEADER_HEIGHT' was not declared in this scope
identifier "HEADER_HEIGHT" is undefined
'SCREEN_Y_OFFSET' was not declared in this scope
identifier "SCREEN_Y_OFFSET" is undefined

所有关于 Menu.cpp 文件。我认为如果我将 Config.h 文件包含在我的 Main.cpp 中的一个标头中,它应该可以工作。即使我直接在 Main.cpp 中包含配置 - 也会发生相同的错误。我能做些什么呢?

编辑:嗯,奇怪的事情正在发生。如果我#include <Config.h>在 Menu.h 中,则配置仅适用于该文件。如果我将其更改为#include "../Config/Config.h",它可以在 Menu.h 和 Menu.cpp 中使用。这是怎么回事?我的文件夹结构 Using<Config.h>是 platformio 的功能。它会自动找到所有库并编译它们。

4

2 回答 2

2

这看起来不正确:

#include <Config.h>

您正在包括Config.h来自系统的包含路径。
我假设您想Config.h从应用程序包含路径中包含一个。

#include "Config.h" // Note the quotes rather than the < >

检查包含路径的位置:

https://stackoverflow.com/a/11946295/14065

查看两组路径之一中是否还有另一个“Config.h”。

另请注意:

这看起来也不对:

#include <stdint.h>

这是为了包括整数类型的 C 版本。这不太可能将这些定义正确地放在std.

<cstdint> 与 <stdint.h>

您应该为 C++ 使用正确的头文件:

#include <cstdint>
于 2018-10-01T17:45:42.167 回答
0

发现了一个问题。原来 Config 可能是保留名称,这就是它不起作用的原因。将名称更改为 CFG 解决了问题。谢谢你的时间。

于 2018-10-01T20:37:18.333 回答