我不熟悉静态类,从我的阅读中可以相信,尽管我得到了很多未定义的引用,但我已经正确设置了静态类。如果有人可以告诉我正确的方法或我做错了什么,我将不胜感激!
头文件:
#ifndef _SYSTEMLOGGING_h
#define _SYSTEMLOGGING_h
#define BUF_LENGTH 45
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "EepromStorage.h"
class SystemLogging
{
public:
static bool writeLog(char logName[5], char timeStamp[9], char itemId[3], char deviceName[11], char value[6], char duration[5], char state[3] );
private:
static char logBuff[BUF_LENGTH];
static char logname[9];
static uint32_t length;
static int lineCount;
static int data;
};
#endif
C ++文件
#include <SdFat.h>
#include <SdFatUtil.h>
#include "SystemLogging.h"
extern SdFile g_file;
extern SdFile g_root;
bool SystemLogging::writeLog(char logName[5], char timeStamp[9], char itemId[3], char deviceName[11], char value[6], char duration[5], char state[3] ){
memset(SystemLogging::logBuff, 0, BUF_LENGTH);
strncpy (SystemLogging::logname, "XXXX.LOG", 9);
strncpy (SystemLogging::logname, logName, 4);
if (!g_file.open(&g_root,SystemLogging::logname, O_RDWR | O_CREAT )) { //create if dosent exist
PgmPrintln("Log Write Error");
return false;
} else {
SystemLogging::length = g_file.fileSize();
g_file.setpos(0);
SystemLogging::lineCount = 0;
SystemLogging::data = 0;
while((data = g_file.read()) > 0)
{
if(data == '\n')
SystemLogging::lineCount++;
}
if(lineCount>99)
g_file.setpos(0);
strcpy(SystemLogging::logBuff,timeStamp + ':');
strcat(SystemLogging::logBuff,itemId + ':');
strcat(SystemLogging::logBuff,deviceName + ':');
strcat(SystemLogging::logBuff,value + ':');
strcat(SystemLogging::logBuff,duration + ':');
strcat(SystemLogging::logBuff,state + '\n');
g_file.print(SystemLogging::logBuff);
g_file.close();
}
return false;
}
编译器错误:
Compiling 'asas' for 'Arduino Mega 2560 or Mega ADK'
SystemLogging.cpp.o : : In function `SystemLogging::writeLog(char*, char*, char*, char*, char*, char*, char*)':
SystemLogging.cpp : logBuff'
SystemLogging.cpp : logBuff'
SystemLogging.cpp : logname'
SystemLogging.cpp : logname'
SystemLogging.cpp : logname'
SystemLogging.cpp : logname'
SystemLogging.cpp : logname'
SystemLogging.cpp.o : :C:\Users\Tim\AppData\Local\VMicro\Arduino\Builds\asas\mega2560\SystemLogging.cpp:16: more undefined references to `SystemLogging::logname' follow
SystemLogging.cpp.o : : In function `SystemLogging::writeLog(char*, char*, char*, char*, char*, char*, char*)':
SystemLogging.cpp : length'
SystemLogging.cpp : length'
SystemLogging.cpp : length'
SystemLogging.cpp : length'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : data'
SystemLogging.cpp : data'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : data'
SystemLogging.cpp : data'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : lineCount'
SystemLogging.cpp : logBuff'
SystemLogging.cpp : logBuff'
SystemLogging.cpp : logBuff'
SystemLogging.cpp : logBuff'
SystemLogging.cpp : logBuff'
SystemLogging.cpp.o : :C:\Users\Tim\AppData\Local\VMicro\Arduino\Builds\asas\mega2560\SystemLogging.cpp:33: more undefined references to `SystemLogging::logBuff' follow
Error creating .elf
谢谢你的帮助!
static
类中成员的声明只是声明,而不是定义。如果您实际上static
以实质性方式使用任何成员,则还需要定义它们。似乎您没有提供定义:
// in your .cpp file add:
char SystemLogging::logBuff[BUF_LENGTH];
char SystemLogging::logname[9];
uint32_t SystemLogging::length;
int SystemLogging::lineCount;
int SystemLogging::data;
您可能还应该初始化变量,因为它们获得的零值可能与所需的值不完全相同。
顺便说一句,这个名字_SYSTEMLOGGING_h
被保留用于由编译器和标准库:你是不是允许使用开始以下划线随后在任何情况下资本性质的任何名称,除非它们被明确允许使用(,例如像__FILE__
) 。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句