-1

I'm trying to create a C++ class which can work as a holder for my project, so have implemented my class members and functions static, but I don't know why compiler can recognize the _nTriggerMode inside the setTriggerMode.

Here is my header file:

#pragma once
class GrabberOptions
{
private:
    static int _nTriggerMode;
    static int _nExposureInMicroSec;
    static double _dFramesPerSecond;
    static int _nExsysncOn;

public:
    GrabberOptions(void);
    ~GrabberOptions(void);
    static void setTriggerMode(int triggerMode);
    static void setExposureInMicroSec(int exposureMicroSec);
    static void setFramePerSecond(double framePerSec);
    static void setExsysncOn(int exsysncOn);

    static int getTriggerMode();
    static int getExposureInMicroSec();
    static double getFramePerSecond();
    static int getExsysncOn();
};

And here is the .ccp file:

#include "StdAfx.h"
#include "GrabberOptions.h"
 int GrabberOptions::_nTriggerMode;

int GrabberOptions::_nExposureInMicroSec; double GrabberOptions::_dFramesPerSecond; int GrabberOptions::_nExsysncOn; GrabberOptions::GrabberOptions(void) { _nTriggerMode = GRABBER_CONTROLLED; _nExposureInMicroSec = 20; _dFramesPerSecond = 1000; _nExsysncOn = 1; }

GrabberOptions::~GrabberOptions(void)
{
}

void setTriggerMode(int triggerMode){ _nTriggerMode=triggerMode; }

Please give me some idea of how to use static.

user261002
  • 2,182
  • 10
  • 46
  • 73
  • 2
    -1. "An error" doesn't describe the problem. Tell what error you get. – Rob Kennedy Apr 03 '12 at 16:12
  • possible duplicate of [What does it mean to have an undefined reference to a static member?](http://stackoverflow.com/questions/7092765/what-does-it-mean-to-have-an-undefined-reference-to-a-static-member) – Flexo Apr 03 '12 at 16:12

3 Answers3

4

You need to initialize the static outside the class definition, in a single translation unit (usually your corresponding implementation file):

#include "StdAfx.h"
#include "GrabberOptions.h"

double GrabberOptions::_dFramesPerSecond; //initializes to 0
//double GrabberOptions::_dFramesPerSecond = 1337; //if you want a different value

GrabberOptions::GrabberOptions(void)
{
    //  _nTriggerMode       = GRABBER_CONTROLLED;
    //    _nExposureInMicroSec  = 20;
        _dFramesPerSecond   = 1000;
    //  _nExsysncOn         = 1;
}

GrabberOptions::~GrabberOptions(void)
{
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • @user261002 not necessarily, methods can be defined inside the class. Constant integrals too (but your member is not). – Luchian Grigore Apr 03 '12 at 16:13
  • now I have the same problem in my .cpp file with the methods. triying to write the setter, but the compile cant recognize the _TriggerMode variable :void setTriggerMode(int triggerMode){ _nTriggerMode=triggerMode; } – user261002 Apr 03 '12 at 16:20
  • @user261002 different issue -> ask a new question. – Luchian Grigore Apr 03 '12 at 16:21
4

static class member variables must be defined outside of the class definition:

// .h file
class GrabberOptions
{
private:
    static double _dFramesPerSecond; // declaration

// .cpp file
double GrabberOptions::_dFramesPerSecond = 1000; // definition
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • hmjd, thank you for your reply. can you please add how can I add my setters too, cause I am trying this code in my .cpp file, void setTriggerMode(int triggerMode){ _nTriggerMode=triggerMode; } but I dont know why there is a red line undet the _nTriggerMode? – user261002 Apr 03 '12 at 16:31
  • Looks as if the function name is not being qualified with the name of the class. Change to: `void GrabberOptions::setTriggerMode(int triggerMode)`. – hmjd Apr 03 '12 at 16:34
1

The static member variables of a class are shared by all instances of a class. They are occasionally useful, but this is probably not an example. Static member functions can only access static member variables.

Your class design shown by the commented out code has no per-instance data; everything is static. That is essentially never a good design in C++.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278