can't link class with static methods and members

  • Thread starter Jean-Francois Brault
  • Start date
J

Jean-Francois Brault

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+0x128): 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;
}
}
 
B

Buster

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top