S
Scott J. McCaughrin
The following program compiles fine but elicits this message from the
linker: "undefined reference to VarArray::funct" and thus fails.
It seems to behave as if the static data-member: VarArray::funct were
an extern, but it is declared in the same file (q.v.). What is the
remedy for this?
=================
#include <iostream.h>
typedef void (*Funct)(int,int); // TYPE OF THE STATIC DATA-MEMBER
class VarArray
{
static Funct funct; // HERE IS WHERE THE STATIC MEMBER IS DECLARED
double* rows; // -> vector of doubles
int order; // length of vector
public:
VarArray(int n) { order = n; rows = new double[order]; }
~VarArray() {}
friend void SetVarArrayHandler(Funct); // WILL INIT STATIC MEMBER
double& operator[](int index)
{
if (index > -1 && index < order) return rows[index];
else (*funct)(index, order); // index < 0 or index > order-1
}
};
void SetVarArrayHandler(Funct fp)
{ VarArray::funct = fp; } // INITS STATIC MEMBER TO: fp (= Quit)
void Quit(int ndx, int len)
{
cerr << "index = " << ndx << " is out of bounds: [0 ... "
<< len-1 << "] -- quitting\n";
exit(-1); // ABEND
}
int main()
{
SetVarArrayHandler(Quit); // WILL INIT STATIC MEMBER TO: Quit
VarArray mat(3); // similar to: VarArray mat[3]
cout << "mat[5] = " << mat[5] << endl; /* should invoke: Quit() */
return 0;
}
linker: "undefined reference to VarArray::funct" and thus fails.
It seems to behave as if the static data-member: VarArray::funct were
an extern, but it is declared in the same file (q.v.). What is the
remedy for this?
=================
#include <iostream.h>
typedef void (*Funct)(int,int); // TYPE OF THE STATIC DATA-MEMBER
class VarArray
{
static Funct funct; // HERE IS WHERE THE STATIC MEMBER IS DECLARED
double* rows; // -> vector of doubles
int order; // length of vector
public:
VarArray(int n) { order = n; rows = new double[order]; }
~VarArray() {}
friend void SetVarArrayHandler(Funct); // WILL INIT STATIC MEMBER
double& operator[](int index)
{
if (index > -1 && index < order) return rows[index];
else (*funct)(index, order); // index < 0 or index > order-1
}
};
void SetVarArrayHandler(Funct fp)
{ VarArray::funct = fp; } // INITS STATIC MEMBER TO: fp (= Quit)
void Quit(int ndx, int len)
{
cerr << "index = " << ndx << " is out of bounds: [0 ... "
<< len-1 << "] -- quitting\n";
exit(-1); // ABEND
}
int main()
{
SetVarArrayHandler(Quit); // WILL INIT STATIC MEMBER TO: Quit
VarArray mat(3); // similar to: VarArray mat[3]
cout << "mat[5] = " << mat[5] << endl; /* should invoke: Quit() */
return 0;
}