Sign in to follow this  
unholymight

Can someone educate me regarding this error?

Recommended Posts

LogWriter.h
class LogWriter : public FileHandler
{
public:
  
  LogWriter();
  ~LogWriter();
  
private:
  
  static const string& getCurrentTime();
  static int b;
};
#include "LogWriter.h" LogWriter.cpp
const string& LogWriter::getCurrentTime()
{
  b = 5;
}
The error is: "Undefined reference to LogWriter::b", and the trigger is that one line in the getCurrentTime() function. I've read many tutorials about static member variables and functions, but why isn't this working?? I'm using Dev-C++, the latest version, by the way.

Share this post


Link to post
Share on other sites
Quote:
Original post by unholymight
The error is: "Undefined reference to LogWriter::b", and the trigger is that one line in the getCurrentTime() function.
You need to explicitly define LogWriter::b, like it says. Add this to your implementation (.cpp) file, right after the #include:
int LogWriter::b;

Share this post


Link to post
Share on other sites
Thanks so much! Wonder why none of these tutorials mentioned it:

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr039.htm
http://www.exforsys.com/tutorials/c-plus-plus/c-plus-plus-static-functions.html

Share this post


Link to post
Share on other sites
Quote:
Original post by unholymight
Thanks so much! Wonder why none of these tutorials mentioned it:

http://www.exforsys.com/tutorials/c-plus-plus/c-plus-plus-static-functions.html


1) You're looking at a tutorial which refers to the non-standard (more than ten years out of date, and "officially never existed") iostream.h.
2) You're looking at a C++ tutorial on a webpage that is also advertising life insurance, credit cards and debt management.

Is it any wonder that the given information is a bit suspect?

3) Sample code is provided and an assertion made about the output. When you're learning something, it's a good idea to test these things out. Oversights of this sort are very easy to make in untested code, even by experts. Details like this can also get lost "in translation" in the editing and publishing process.

Quote:
http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr039.htm


Um, it's kind of right there:

Quote:


struct X {
private:
int i;
static int si;
public:
void set_i(int arg) { i = arg; }
static void set_si(int arg) { si = arg; }

void print_i() {
cout << "Value of i = " << i << endl;
cout << "Again, value of i = " << this->i << endl;
}

static void print_si() {
cout << "Value of si = " << si << endl;
// cout << "Again, value of si = " << this->si << endl;
}

};

int X::si = 77; // Initialize static data member



(Emphasis mine.)

But more importantly, you've linked to the section on static member functions, when the question is about a static data member - which incidentally, is covered in the immediately previous section. And there, the explanation could hardly be more explicit:

Quote:

The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope. For example:

class X
{
public:
static int i;
};
int X::i = 0; // definition outside class declaration

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this