Pages: [1]   Go Down
Author Topic: Creating a static object  (Read 3127 times)
0 Members and 1 Guest are viewing this topic.
UK
Offline Offline
Jr. Member
**
Karma: 2
Posts: 90
It was like it when I found it
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having trouble creating a static object. Take a look at this snippet from a header file...
Code:
#include <stdlib.h>       #include "WProgram.h"    
#include "Logger.h" // this file
#include <PrivateLogger.h>


class Logger
{
  public:
    Logger();                        // Constructor
    void writeLine(char * line);     // Write a line of data to the log file
    void close();                    // Close the file

  private:
    // Member variables
    PrivateLogger m_logger;
};

This code currently works as shown. It will compile and run without problems. As soon as I try to make my object static like this.....
Code:
 private:
    // Member variables
    static PrivateLogger m_logger;

Then I get the follwoing error
Quote
Logger\Logger.cpp.o: In function `Logger::close()':
C:\all_apps\arduino-0021\libraries\Logger/Logger.cpp:57: undefined reference to `Logger::m_logger'
C:\all_apps\arduino-0021\libraries\Logger/Logger.cpp:57: undefined reference to `Logger::m_logger'
Logger\Logger.cpp.o: In function `Logger::writeLine(char*)':
C:\all_apps\arduino-0021\libraries\Logger/Logger.cpp:50: undefined reference to `Logger::m_logger'
C:\all_apps\arduino-0021\libraries\Logger/Logger.cpp:50: undefined reference to `Logger::m_logger'

The code it's actually complaining about is listed here....
Code:
// Constructor
Logger::Logger()
{
}

void Logger::writeLine(char * line)    // Write a line of data to the log file
{
  m_logger.writeLine(line);
}

void Logger::close()    
{
  m_logger.close();
}

My Logger object is going to be used within lots of different classes within my sketch, so I'm trying to use the static keyword to force only a single instance of the logger class.

I'm trying to do this for two reasons, firstly since I only have one SD card and one log file it makes sense to only have a single object accessing it and, although it is possible to have multiple objects writing to the same file, whichever object closes the file first (and thus 'commits' whatever it's written) get to overwrite anything that any other object was writting.

Basically I have a couple of questions....
1) is it possible to implement a singleton class in arduino? If so, how?
2) Is my approach correct or not? and if it is why isn't it working and how do I fix it?

Cheers

Mike
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why does m_logger need to be static?

The m_logger variable should really be a pointer to an instance of PrivateLogger. The implementation file should create (if needed) or return (if existing) that instance.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 2
Posts: 90
It was like it when I found it
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the reply Paul, I'm pushing (as usual) at the boundaries of my coding knowledge.

Quote
Why does m_logger need to be static
I understood that I need to make it static to ensure that there's only one instance of it? is this not the case?

Quote
The implementation file should create (if needed) or return (if existing) that instance
I know! This is what I'm trying to achieve, the question is, how do I do it?

Can you tell me how to do this, or give me a code example, or point me to some documentation (or another thread) than can give me some clues?

Any help appreciated!

Mike
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think what I would do is make m_logger a pointer, non-static. Then, make a static member to count the number of instances of the Logger class. When the first instance is created, create an instance of the PrivateLogger class. When subsequent instances are created, don't.
Logged

UK
Offline Offline
Jr. Member
**
Karma: 2
Posts: 90
It was like it when I found it
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul,

can you point me in the direction of any code examples for this sort of thing?

Cheers
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 74
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have never gotten any static members of objects (even basic standard types like int) to compile successfuly... What I have done is put anything that needs to be a single instance for any class as a global variable scoped only to my code files which contain the object and methods...  its a hack but it does work.  
Logged

Pages: [1]   Go Up
Jump to: