【发布时间】:2019-03-06 19:35:43
【问题描述】:
我对以下代码有疑问(这些只是 sn-ps,不是完整文件):
菜单.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);
}
}
配置.h
#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 中包含配置 - 也会发生相同的错误。我该怎么办?
编辑:
嗯,奇怪的事情正在发生。如果我在 Menu.h 中有 #include <Config.h>,则配置仅适用于该文件。如果我将其更改为#include "../Config/Config.h",它可以在 Menu.h 和 Menu.cpp 中使用。这是怎么回事? My folder structure
使用<Config.h> 是platformio 的功能。它会自动找到所有库并编译它们。
【问题讨论】:
-
为什么是
constexpr而不是const只是简单的常量值? -
都试过了。两者都不起作用。我只是认为 constexpr 在我的情况下可能会更好。
-
我的猜测 - 其他一些标头已经定义了 CONIFG_H,试试 CONFIG___H
-
正如@pm100 指出的那样,您可能在这里遇到命名空间冲突。现在是 2018 年,您可能还想检查您的编译器是否支持
#pragma once以避免所有这些#ifndef废话。 -
如果它不理解自 1970 年代以来一直存在的
"..."风格,该扩展听起来要么配置错误,要么超级损坏。
标签: c++ arduino platformio