By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

address of static const

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

Thanks.
Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
cppsks wrote:
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;

Jul 23 '05 #2

P: n/a
cppsks wrote:
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
Jul 23 '05 #3

P: n/a
cppsks wrote:
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.
Jul 23 '05 #4

P: n/a
cppsks wrote:
Taking the address of a static const resulted in a unresolved symbol.
What kind of static const? Where is it defined? How? Where do you take the
address?
Why is that?
Because a semicolon in line 42 is missing.
Is the address assigned at load time?


What do you mean by that?

Jul 23 '05 #5

P: n/a
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cu*************@news.t-online.com...
cppsks wrote:
Taking the address of a static const resulted in a unresolved symbol.
What kind of static const? Where is it defined? How? Where do you take the
address?
Why is that?


Because a semicolon in line 42 is missing.
Is the address assigned at load time?


What do you mean by that?


"Rolf Magnus" <ra******@t-online.de> wrote in message
news:<cu*************@news.t-online.com>... cppsks wrote:
Taking the address of a static const resulted in a unresolved symbol.


What kind of static const? Where is it defined? How? Where do you take the
address?
Why is that?


Because a semicolon in line 42 is missing.
Is the address assigned at load time?


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
Jul 23 '05 #6

P: n/a
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)
{
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

Jul 23 '05 #7

P: n/a
Thomas Matthews wrote:
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?
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.


Jul 23 '05 #8

P: n/a
E. Robert Tisdale wrote:
Thomas Matthews wrote:
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.

Jul 23 '05 #9

P: n/a
"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cu*************@news.t-online.com...
E. Robert Tisdale wrote:
Thomas Matthews wrote:
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.

Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.