address of static const

C

cppsks

Taking the address of a static const resulted in a unresolved symbol. Why is
that? Is the address assigned at load time?

Thanks.
 
J

Jonathan Turkanis

cppsks said:
Taking the address of a static const resulted in a unresolved symbol.
Why is that? Is the address assigned at load time?

If you want to take the address of a static const integral data member with an
in-class initializer, you need to provide an (out-of-class) definition.

Jonathan
 
G

Gianni Mariani

cppsks said:
Taking the address of a static const resulted in a unresolved symbol. Why is
that? Is the address assigned at load time?


Let's see the code.

e.g.

struct A
{
static const float B;
};

const float A::B = 3.3;
 
E

E. Robert Tisdale

cppsks said:
Taking the address of a static const resulted in a unresolved symbol.
Why is that?
Is the address assigned at load time?
cat main.cc
#include <iostream>

static const
int a = 13; // in main.cc

int
main(int argc, char* argv[]) {
if (0 != &a)
std::cout << "a = " << a << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main
a = 13

Works fine for me.
 
C

cppsks

Rolf Magnus said:
What kind of static const? Where is it defined? How? Where do you take the
address?


Because a semicolon in line 42 is missing.


What do you mean by that?

Rolf Magnus said:
What kind of static const? Where is it defined? How? Where do you take the
address?


Because a semicolon in line 42 is missing.


What do you mean by that?

Here is the code and the compilation issue:

#include <iostream>
class hi
{
public:
static const int constant = 10;
static void printMe(const int* a)
{
cout << "*a" << endl;
}
};

int main()
{
hi::printMe(&hi::constant);
}

:/>g++ addrStaticConst.cc
Undefined first referenced
symbol in file
hi::constant /tmp/ccyC1JeP.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
 
T

Thomas Matthews

cppsks said:
Here is the code and the compilation issue:

#include <iostream>
class hi
{
public:
static const int constant = 10;
static void printMe(const int* a)
{
cout << "*a" << endl;
}
};

int main()
{
hi::printMe(&hi::constant);
}

:/>g++ addrStaticConst.cc
Undefined first referenced
symbol in file
hi::constant /tmp/ccyC1JeP.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

You need to specify an instance of the variable:
class hi
{
public:
/* Note the removal of the assignment */
static const int constant;
static void printMe(const int* a)
{
cout << "*a" << endl;
}
};

/* Here is the instance */
const int hi::constant = 10;

int main()
{
hi::printMe(&hi::constant);

/* Functions declared as returning a value
* should return a value. */
return 0;
}

BTW, the instance of the constant should not be
in a header file. Read the FAQs below for more
information.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
E

E. Robert Tisdale

Thomas said:
cppsks said:
Here is the code and the compilation issue:

#include <iostream>
class hi {
public:
static const int constant = 10;
static void printMe(const int* a) {
std::cout << "*a" << std::endl;
}
};

int main(int argc, char* argv[]) {
hi::printMe(&hi::constant); return 0;
}

:/>g++ addrStaticConst.cc
Undefined first referenced
symbol in file
hi::constant /tmp/ccyC1JeP.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
You need to specify an instance of the variable:

Can anybody cite (and quote) the passage
from the ANSI/ISO C++ standard document that requires this?
I'm still suspicious that this may be a bug in g++.
class hi {
public:
/* Note the removal of the assignment */
static const int constant;
static void printMe(const int* a) {
std::cout << "*a" << std::endl;
}
};

// Here is the instance
const int hi::constant = 10;

int main(int argc, char* argv[]) {
hi::printMe(&hi::constant);

// Functions declared as returning a value
// should return a value.
return 0;
}

BTW, the instance of the constant
should not be in a header file.
Read the FAQs below for more information.
 
R

Rolf Magnus

E. Robert Tisdale said:
Thomas said:
cppsks said:
Here is the code and the compilation issue:

#include <iostream>
class hi {
public:
static const int constant = 10;
static void printMe(const int* a) {
std::cout << "*a" << std::endl;
}
};

int main(int argc, char* argv[]) {
hi::printMe(&hi::constant); return 0;
}

:/>g++ addrStaticConst.cc
Undefined first referenced
symbol in file
hi::constant /tmp/ccyC1JeP.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
You need to specify an instance of the variable:

Can anybody cite (and quote) the passage
from the ANSI/ISO C++ standard document that requires this?

9.4.2 Static data members

If a static data member is of const integral or const enumeration type, its
declaration in the class definition can specify a constant-initializer
which shall be an integral constant expression (5.19). In that case, the
member can appear in integral constant expressions within its scope. The
member shall still be defined in a namespace scope if it is used in the
program and the namespace scope definition shall not contain an
initializer.
I'm still suspicious that this may be a bug in g++.

It's not.
 
C

cppsks

Rolf Magnus said:
E. Robert Tisdale said:
Thomas said:
cppsks wrote:

Here is the code and the compilation issue:

#include <iostream>
class hi {
public:
static const int constant = 10;
static void printMe(const int* a) {
std::cout << "*a" << std::endl;
}
};

int main(int argc, char* argv[]) {
hi::printMe(&hi::constant); return 0;
}

:/>g++ addrStaticConst.cc
Undefined first referenced
symbol in file
hi::constant /tmp/ccyC1JeP.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

You need to specify an instance of the variable:

Can anybody cite (and quote) the passage
from the ANSI/ISO C++ standard document that requires this?

9.4.2 Static data members

If a static data member is of const integral or const enumeration type, its
declaration in the class definition can specify a constant-initializer
which shall be an integral constant expression (5.19). In that case, the
member can appear in integral constant expressions within its scope. The
member shall still be defined in a namespace scope if it is used in the
program and the namespace scope definition shall not contain an
initializer.
I'm still suspicious that this may be a bug in g++.

It's not.

Based on the information that you provided from the standard, your solution
works as well as the following:
#include <iostream>
class hi
{
public:
static const int constant = 10; // **********
static void printMe(const int* a)
{
cout << "*a" << endl;
}
};

const int hi::constant; // **************
int main()
{
hi::printMe(&hi::constant);
}


Saying that, I would also state that your solution is much better than the
one presented above.
 

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

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,650
Latest member
IanTylor5

Latest Threads

Top