• Advertisement
Sign in to follow this  

Can someone educate me regarding this error?

This topic is 3393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
In one source file at namespace scope (not inside a function or class definition) you need to place

int LogWriter::b;


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
Sign in to follow this  

  • Advertisement