Archived

This topic is now archived and is closed to further replies.

FrigidHelix

static member data

Recommended Posts

I have no idea why this isn''t working, since I have done something like it before, but each time I try to access a static member, i get the following link error:
quote:
error LNK2001: unresolved external symbol "public: static long CFraction::MaxNumerator" (?MaxNumerator@CFraction@@1JA)
Here is a stripped down version of the class I am using. It is defined in a header file.
  class CFraction
{
protected:	
	long	Numerator;
	long	Denominator;
public:
	
	CFraction() { Numerator=Denominator=1;}

	static long MaxNumerator;
	static long MaxDenominator;
};  
Now, in my main cpp file I try and set the static member MaxNumerator using the following code:
  long CFraction::MaxNumerator = 10L;  
And, as aforementioned, I get a linker error. Any ideas?

Share this post


Link to post
Share on other sites
I can''t seem to be able to repeat the link error.

Might I suggest you post your entire code (for both CFraction.h, main.cpp and if you have it CFraction.cpp (or where ever you define your CFraction functions).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
the static variable must not be in main.cpp (i think)

btw, put your code here

Share this post


Link to post
Share on other sites
I created a test project, below is its entire code. This is ridiculous!!! WHY THE #%^#$# won't this work? I copied the code almost exactly from a C++ book (Practical C++)


    
#include <iostream.h>

class myClass
{
public:
static int var;

static void setVar( int i )
{
var = i;
}
};


int main()
{
// Doesnt work. I get the compiler error:

// definition or redeclaration illegal in current scope

int myClass::var = 1;

// Compiles, but I get linker error:

// unresolved external symbol

// "public: static int myClass::var" (?var@myClass@@2HA)

myClass::setVar( 1 );


return 0;
}


[edited by - FrigidHelix on March 22, 2002 10:14:17 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It would seem that you forgot to define the static member. Here is the template for using a static member:


  
// header file ''SomeClass.h''

class SomeClass
{
public:
// stuff

private:
static int i;
// stuff

};

// implementation file ''SomeClass.cpp''

#include "SomeClass.h"
int someClass::i=0; // Initialize to a value is optional


// driver file ''MyProg.cpp''

#include <iostream>
using namespace std;
#include "SomeClass.h"

int main()
{
SomeClass::i=5;
cout << someClass::i;
return 0;
}


-D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Clarification -- Initializing to a value in the implementation file is optional. Defining there is not. So it has to be in one of the following two forms:

int SomeClass::i=0;

or

int SomeClass::i;

Sorry for the poor formatting of my code example in the previous post. I am still getting used to the interface.

-D

Share this post


Link to post
Share on other sites
thanks a lot. I finally understand my fault: I need to move the definition of the static variable outside of any functions. It was in fact an incorrect scope. thanks guys.

Share this post


Link to post
Share on other sites