Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > can't link class with static methods and members

Reply
Thread Tools

can't link class with static methods and members

 
 
Jean-Francois Brault
Guest
Posts: n/a
 
      04-17-2004
I wrote a crappy class for radian angle management. The class consists
of an array of radian values. I put all these things in a class in
which all methods are static, so I can access it anywhere in my code
(I could have made a library instead of a class).

I can't find out why g++ compiles fine, but does not link correctly
the following code. Here, I am posting my full code, .h and .cpp just
to make all things clear.

The linker output many errors. Some on them are:
/home/jf/tmp/ccEuf6Sy.o(.text+0x12: In function
`Angle::getRadValue(int)':
: undefined reference to `Angle::ra'
/home/jf/tmp/ccEuf6Sy.o(.text+0x13f): In function
`Angle::isOver(int)':
: undefined reference to `Angle::max'
/home/jf/tmp/ccEuf6Sy.o(.text+0x165): In function
`Angle::isUnder(int)':
: undefined reference to `Angle::min'

I can't figure out why, for instance, the function Angle::isOver(int)
can't find Angle::max. Angle::max was defined as static, so I think it
should be accessible, but it is not.

From what I know, the Angle::max is static, so there is only one
instance of this member, no matter how many Angle implantation there
are, so it should be accessible... isn't?

Anyone knows?

//Class definition
class Angle
{
public:

Angle();

~Angle();

static float getRadValue( int _i);

static bool isOver( int _i);
static bool isUnder( int _i);

static int increment( int _i);
static int decrement( int _i);

static int getIndiceFromDeg( int _deg);


static float ra[11];
static int min; //min indice
static int max; //max indice


};


//Class implementation
#include "angle.h"

/************************************************** **************************
* function Angle
* Default constructor
* Return: None
************************************************** **************************/
Angle::Angle() {
Angle::ra[0] = -1.308996939;
Angle::ra[1] = -1.047197551;
Angle::ra[2] = -0.785398163;
Angle::ra[3] = -0.523598775;
Angle::ra[4] = -0.261799387;
Angle::ra[5] = 0.0;
Angle::ra[6] = 0.261799387;
Angle::ra[7] = 0.523598775;
Angle::ra[8] = 0.785398163;
Angle::ra[9] = 1.047197551;
Angle::ra[10] = 1.308996939;

Angle::min = 0;
Angle::max = 10;
}

/************************************************** **************************
* function ~Angle
* This is the default destructor
* Input: None
* Return: None
************************************************** **************************/
Angle::~Angle() {
}

/************************************************** **************************
* function getRadValue
* This function returns the angle in radian from the indice
* Input: Angle indice
* Return: Angle in rad
************************************************** **************************/
float Angle::getRadValue( int _i) {
return Angle::ra[_i];
}

/************************************************** **************************
* function isOver
* This function checks if the angle indice is too high
* Input: Angle indice
* Return: Success
************************************************** **************************/
bool Angle::isOver( int _i) {
if (_i > Angle::max) {
return true;
}
return false;
}

/************************************************** **************************
* function isUnder
* This function checks if the angle indice is too low
* Input: Angle indice
* Return: Success
************************************************** **************************/
bool Angle::isUnder( int _i) {
if (_i < Angle::min) {
return true;
}
return false;
}

/************************************************** **************************
* function increment
* This function moves to the next angle, upper
* Input: Angle indice
* Return: New angle indice
************************************************** **************************/
int Angle::increment( int _i) {
if (Angle::isOver(_i + 1)) {
return _i;
}
return (_i + 1);
}

/************************************************** **************************
* function decrement
* This function moves to the next angle, lower
* Input: Angle indice
* Return: New angle indice
************************************************** **************************/
int Angle::decrement( int _i) {
if (Angle::isUnder(_i - 1)) {
return _i;
}
return (_i - 1);
}

/************************************************** **************************
* function getIndiceFromDeg
* This function returns the angle indice from its degree value
* Input: Angle in degree
* Return: Angle indice
************************************************** **************************/
int Angle::getIndiceFromDeg( int _deg) {
switch (_deg) {
case -75: return 0;
break;
case -60: return 1;
break;
case -45: return 2;
break;
case -30: return 3;
break;
case -15: return 4;
break;
case 0: return 5;
break;
case 15: return 6;
break;
case 30: return 7;
break;
case 45: return 8;
break;
case 60: return 9;
break;
case 75: return 10;
break;
}
}
 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      04-17-2004
Jean-Francois Brault wrote:

The declaration in a class of a static data member doesn't count as a
definition. There has to be a separate definition in exactly one
translation unit (in your case, the same file where you define the
member functions of Angle), like this:

float Angle::ra [10];
int Angle::min;
int Angle::max;

--
Regards,
Buster.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
Difference between static final members and final static members(if any)? JFCM Java 4 02-07-2006 11:32 AM
Instantiating a static class( Class with all static members - methods and variables) SaravanaKumar Java 6 10-19-2004 08:20 AM
static class methods and data members Neil Zanella Python 1 08-18-2004 10:20 PM
static member functions access to class members and methods Roger Leigh C++ 11 01-22-2004 12:49 PM



Advertisments