A
Amadeus W. M.
I have a member static const int x defined in class Foo, and I'm passing
it by reference, and by value elsewhere (see the code below). Passing it
by value works, but by reference it doesn't: it thinks x is undefined.
Could someone explain what's going on here? Why can't I pass a static
const member by reference?
This is how I compile it:
g++ -g -Wall sample_main.C # g++ -v 4.0.1
/tmp/ccUJx59K.o(.gnu.linkonce.t._ZN3Foo12bad_functionER3Bar[Foo::bad_function(Bar&)]+0xa): In function `Foo::bad_function(Bar&)':
/tmp/STATICCONST/sample_main.C:18: undefined reference to `Foo::x'
collect2: ld returned 1 exit status
#include <cstdlib>
using namespace std;
class Bar
{
public:
void good(const int a) {}
void bad(const int & a) {}
};
class Foo
{
public:
static const int x=0;
void good_function(class Bar & B){ B.good(x); }
void bad_function(class Bar & B) { B.bad(x); }
};
int main(int argc, char * argv[])
{
Foo F;
Bar B;
F.good_function(B);
F.bad_function(B);
return 0;
}
it by reference, and by value elsewhere (see the code below). Passing it
by value works, but by reference it doesn't: it thinks x is undefined.
Could someone explain what's going on here? Why can't I pass a static
const member by reference?
This is how I compile it:
g++ -g -Wall sample_main.C # g++ -v 4.0.1
/tmp/ccUJx59K.o(.gnu.linkonce.t._ZN3Foo12bad_functionER3Bar[Foo::bad_function(Bar&)]+0xa): In function `Foo::bad_function(Bar&)':
/tmp/STATICCONST/sample_main.C:18: undefined reference to `Foo::x'
collect2: ld returned 1 exit status
#include <cstdlib>
using namespace std;
class Bar
{
public:
void good(const int a) {}
void bad(const int & a) {}
};
class Foo
{
public:
static const int x=0;
void good_function(class Bar & B){ B.good(x); }
void bad_function(class Bar & B) { B.bad(x); }
};
int main(int argc, char * argv[])
{
Foo F;
Bar B;
F.good_function(B);
F.bad_function(B);
return 0;
}