当我尝试声明包含在 rckt_test.ino(等于 .cpp)中的 log_.h 时,编译器给我这个异常:rckt_test.ino: 9:6: error : 'log_ log' 重新声明为不同类型的符号
log_日志
。但我以前从未在其他任何地方声明过这个类。甚至可以在函数外声明 log_ 类吗?
rckt_test.ino(.cpp):
#include <Arduino.h>
#include "log_.h"
// Settings
bool log_file = true; // Depends on availabilty of SD Cart reader
// Defining log class
log_ log;
void setup() {
Serial.begin(9600);
log.log_init(log_file);
log.log_text("Rocket startup");
}
void loop() {
}
log_.h:
#pragma once
#include <Arduino.h>
class log_ {
private:
void print_text(String text);
void save_to_log_file(String text);
static bool log_in_file;
public:
void log_init(bool file_loggging);
void log_text(String text);
};
错误:
Compiling debug version of 'rckt_test' for 'Arduino Nano w/ ATmega328P'
rckt_test.ino: 9:6: error: 'log_ log' redeclared as different kind of symbol
log_ log
arduino.h:26: In file included from
rckt_test.ino: from
math.h:305: note previous declaration double log(double)
extern double log(double __x) __ATTR_CONST__
rckt_test.ino: In function void setup()
rckt_test.ino: 15:5: error: request for member 'log_init' in 'log', which is
of non-class type 'double(double)
log.log_init(log_file)
rckt_test.ino: 17:5: error: request for member 'log_text' in 'log', which is
of non-class type 'double(double)
log.log_text("Rocket startup")
log_.cpp: 29:42: error: no 'void log_::log_in_file_setter(bool)' member
function declared in class 'log_
void log_*: log_in_file_setter(bool setter) {
log_.cpp: 33:31: error: no 'bool log_::log_in_file_getter()' member function
declared in class 'log_
bool log_*: log_in_file_getter() {
Error compiling project sources
Debug build failed for project 'rckt_test'
最佳答案
错误显示log
的另一个声明来自math.h
,这是数学对数的函数声明。将变量从 log
重命名为其他名称或将其包装在不同的 namespace
中。
关于C++|在函数外声明类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49510591/