problem passing a static const int member by reference

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;
}
 
G

Gianni Mariani

Amadeus W. M. wrote:
....
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); }
};

Add this:

const int Foo::x;
 
J

John Harrison

Amadeus said:
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?

You can but the difference is that when you pass by value the compiler
substitutes the value 0 so storage for the actual variable is not
needed. But when you pass by reference you need an actual variable which
you have failed to declare (what is in the class is a definition not a
declaration).

You need to add the declaration to sample_main.C as Gianni showed.

john
 
J

John Harrison

John said:
You can but the difference is that when you pass by value the compiler
substitutes the value 0 so storage for the actual variable is not
needed. But when you pass by reference you need an actual variable which
you have failed to declare (what is in the class is a definition not a
declaration).

You need to add the declaration to sample_main.C as Gianni showed.

john

Sorry, got the words definition and declaration completely the wrong way
round in the above explanation.

john
 
A

Amadeus W. M.

Amadeus W. M. wrote:
...

Add this:

const int Foo::x;

Of course, this works, but I thought only non-integer static consts
require this. I thought static const int can be defined inside the class.
And it can be defined inside the class, as long I don't pass it by
reference.
 
A

Amadeus W. M.

You can but the difference is that when you pass by value the compiler
substitutes the value 0 so storage for the actual variable is not
needed. But when you pass by reference you need an actual variable which
you have failed to declare (what is in the class is a definition not a
declaration).

But isn't x supposed to be _defined_ (hence allocated and initialized)
when I say

static const int x=0; // inside class Foo;

?? Is it that the compiler is trying to be smart, or am I not complying
with the standard?
 
J

Jon Slaughter

#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;
}


This code works fine under VS8 ;/
 
A

Amadeus W. M.

#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;
}


This code works fine under VS8 ;/

Thank you, maybe I should ask on a g++ newsgroup then.
 
A

Amadeus W. M.

In fact, here's what I want to do: I have 1 header file Foo.H included in
two source files sample_main.C and foobar.C. The definition of Foo::x
should really be in Foo.H.


// file Foo.H:
#ifndef _FOO_H
#define _FOO_H

#include <cstdlib>
using namespace std;

class Bar
{
public:
void good(const int a) const {}
void bad(const int & a) const {}
};


class Foo
{
public:
static const int x=0;

void good_function(const Bar & B) const { B.good(x); }
void bad_function(const Bar & B) const { B.bad(x); }
};


const int Foo::x;

#endif





// file foobar.C:
#include <cstdlib>
#include "Foo.H"

using namespace std;

void foobar()
{
Foo F;
Bar B;

F.good_function(B);
F.bad_function(B);
}





// file sample_main.C
#include <cstdlib>
#include "Foo.H"

using namespace std;

extern void foobar();

int main(int argc, char * argv[])
{
Foo F;
Bar B;

F.good_function(B);
F.bad_function(B);

foobar();

return 0;
}

g++ sample_main.C foobar.C

gives (understandably) multiple definition of Foo::x. Of course, if I put
const int Foo::x;
in sample_main.C, it works, but I it's more natural that the definition of
Foo::x should be in Foo.H.

If I don't put const int Foo::x; anywhere (neither in main nor in Foo.H),
it works, as long as I don't call Foo::bad_function() i.e. as long as I
don't access Foo::x by reference.




If, on the other hand, I make Foo a template class and define

template <class DummyType>
const int Foo<DummyType>::x;

outside Foo in Foo.H, it works (no multiple definition of
Foo<DummyType>::x, no undefined references). I would rather not make Foo
a template class though, if I don't have to.

Is there another way? Thanks!
 
J

John Harrison

Amadeus said:
In fact, here's what I want to do: I have 1 header file Foo.H included in
two source files sample_main.C and foobar.C. The definition of Foo::x
should really be in Foo.H.


// file Foo.H:
#ifndef _FOO_H
#define _FOO_H

#include <cstdlib>
using namespace std;

class Bar
{
public:
void good(const int a) const {}
void bad(const int & a) const {}
};


class Foo
{
public:
static const int x=0;

void good_function(const Bar & B) const { B.good(x); }
void bad_function(const Bar & B) const { B.bad(x); }
};


const int Foo::x;

#endif





// file foobar.C:
#include <cstdlib>
#include "Foo.H"

using namespace std;

void foobar()
{
Foo F;
Bar B;

F.good_function(B);
F.bad_function(B);
}





// file sample_main.C
#include <cstdlib>
#include "Foo.H"

using namespace std;

extern void foobar();

int main(int argc, char * argv[])
{
Foo F;
Bar B;

F.good_function(B);
F.bad_function(B);

foobar();

return 0;
}

g++ sample_main.C foobar.C

gives (understandably) multiple definition of Foo::x. Of course, if I put
const int Foo::x;
in sample_main.C, it works, but I it's more natural that the definition of
Foo::x should be in Foo.H.

Well that is tough, C++ says it should be in sample_main.C (or foobar.C).
If I don't put const int Foo::x; anywhere (neither in main nor in Foo.H),
it works, as long as I don't call Foo::bad_function() i.e. as long as I
don't access Foo::x by reference.

That is true, although I'm not sure the C++ standard guarantees that.
If, on the other hand, I make Foo a template class and define

template <class DummyType>
const int Foo<DummyType>::x;

outside Foo in Foo.H, it works (no multiple definition of
Foo<DummyType>::x, no undefined references). I would rather not make Foo
a template class though, if I don't have to.

Well that is because the rules for templates are different. Template
code *always* goes in header files.
Is there another way? Thanks!

No, unless an enum would suffice.

class Foo
{
enum { x = 1 };
};

In the old days you would have been required to define and initialise
Foo::x in a .C file. Since standardisation you can now initialise Foo::x
in the header file, but you are still required to define it in a .C file.

john
 
J

Jon Slaughter

Try adding #pragma once to the top of your foo.h, it seems that your
ifndef/define isn't working like you think. This fixed the problem for me.

As far as I could tell the compiler was parsing foo.h in foobar.c and
created the object Foo::x then tried to do it again in main.cpp.

By telling it only to parse it once solved the problem I had with multiple
link defines.

Jon
 
J

Jon Slaughter

Jon Slaughter said:
Try adding #pragma once to the top of your foo.h, it seems that your
ifndef/define isn't working like you think. This fixed the problem for me.

As far as I could tell the compiler was parsing foo.h in foobar.c and
created the object Foo::x then tried to do it again in main.cpp.

By telling it only to parse it once solved the problem I had with multiple
link defines.

Jon

heh, nm, it stopped working after I build it again ;/ not sure whats going
on ;/
 
J

Jon Slaughter

Jon Slaughter said:
heh, nm, it stopped working after I build it again ;/ not sure whats going
on ;/

Its definately from it including foo twice.

Compiling...

foobar.cpp

c:\documents and settings\jon\my documents\visual studio
2005\projects\test3\test3\foo.h(32) : error C2720: 'Foo::x' : 'static '
storage-class specifier illegal on members

c:\documents and settings\jon\my documents\visual studio
2005\projects\test3\test3\foo.h(32) : error C2734: 'Foo::x' : const object
must be initialized if not extern

Generating Code...

Compiling...

main.cpp

c:\documents and settings\jon\my documents\visual studio
2005\projects\test3\test3\foo.h(32) : error C2720: 'Foo::x' : 'static '
storage-class specifier illegal on members

c:\documents and settings\jon\my documents\visual studio
2005\projects\test3\test3\foo.h(32) : error C2734: 'Foo::x' : const object
must be initialized if not extern

Generating Code...





Note, the simpliest code here seems to generate the error:



------------ Foo.h -------------

#pragma once

#ifndef _FOO_

#define _FOO_



class Bar

{

public:

void good(const int a) const { }

void bad(const int & a) const { }

};



class Foo

{

public:

static const int x = 3;

void good_function(const Bar &B) const { B.good(x); }

void bad_function(const Bar &B) const { B.bad(x); }

};



const int Foo::x;

#endif



------------- foobar.cpp -----------

#include "Foo.H"



void foobar()

{

int y = Foo::x;

};





---------- Main.cpp -----------

#include "Foo.H"



int main(int argc, char * argv[])

{

int y = Foo::x;

return 0;

}





Note that when I remove the const int Foo::x I have no problems with the
original code(B.bad works fine).

Jon
 
A

Amadeus W. M.

I guess we couldn't see the forest because of the trees. In this trivial
example it makes sense to implement everything in Foo.H, but, as we were
taught in school, we should really separate the interface (in Foo.H) from
the implementation (Foo.C). Then we could have const int Foo::x; in Foo.C,
and that will only be processed once. It's trivial, and it re-invents the
wheel, but it's another good, non-cosmetic reason to separate Foo.H from
Foo.C.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top