can't link class with static methods and members

Discussion in 'C++' started by Jean-Francois Brault, Apr 17, 2004.

  1. 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;
    }
    }
     
    Jean-Francois Brault, Apr 17, 2004
    #1
    1. Advertising

  2. Jean-Francois Brault

    Buster Guest

    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.
     
    Buster, Apr 17, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,410
    Tony Morris
    Oct 19, 2004
  2. JFCM
    Replies:
    4
    Views:
    5,741
  3. CoolPint
    Replies:
    8
    Views:
    989
    Jeff Schwab
    Dec 14, 2003
  4. Roger Leigh
    Replies:
    11
    Views:
    3,376
    Jeff Schwab
    Jan 22, 2004
  5. Neil Zanella

    static class methods and data members

    Neil Zanella, Aug 18, 2004, in forum: Python
    Replies:
    1
    Views:
    289
    Lawrence Oluyede
    Aug 18, 2004
Loading...

Share This Page