C++|在函数外声明类

标签 c++ visual-studio arduino

当我尝试声明包含在 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/

相关文章:

c++ - 定义 BLECharacteristic 时 Genuino 101 不运行

c++ - Cocos2dx : Deleting kinematic bodies once they are off screen

c++ - WinRT 在自定义路径创建文件

c++ - 段错误描述解释

c++ - 如何在 VS2008 中指定 64 位 unsigned int const 0x8000000000000000?

visual-studio - 使用 Entity Framework 时在实体中重写ToString,等于..

azure - 使用 MQTT 通过 Websockets(端口 443)而不是端口 8883 将 ESP32 连接到 Azure Iot Hub

c - 如果不在 loop() 中,Arduino Serial.println() 输出一个空行

c++ - "String iterator not dereferncable" boost async_read_some

visual-studio - 缺少项目/项目模板