1

私は静的クラスに不慣れで、未定義の参照の長いリストを取得しても、正しくセットアップされていると信じています。誰かが私に正しいアプローチや私が間違っていることを教えてくれたら、本当に感謝しています!

ヘッダファイル:

 #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

ご協力いただきありがとうございます!

4

2 に答える 2

4

クラス内のメンバーの宣言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__) .

于 2013-09-16T02:21:09.343 に答える
1

ヘッダー ファイルで静的メンバーを宣言しただけで、.cpp ファイルで定義する必要があります。

char SystemLogging::logBuff[BUF_LENGTH] = {};
char SystemLogging::logname[9] = {};
uint32_t SystemLogging::length = 0;
int SystemLogging::lineCount = 0;
int SystemLogging::data = 0;

実際には、SystemLogging静的メンバーとメンバー関数のみが含まれているため、名前空間を使用することをお勧めします。そして、私はstd::string上を選びますchar array

于 2013-09-16T02:20:14.523 に答える