473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

address of static const

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
9 3217
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
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
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
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
"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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Gidraz | last post by:
Hello, i can't figure out how to retrieve clients MAC address using C#.NET. I can get IP but not MAC. Thanks in advance. Gidraz
9
by: Rob | last post by:
I am trying to write a program with VC++ 6.0 to read a txt file which looks like this: Network Destination Netmask Gateway Interface Metric 0.0.0.0 0.0.0.0 ...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
1
by: silverburgh.meryl | last post by:
I am trying to convert this code from java to c++: public final class Type { public static final int DEFAULT = 1; private static int index = 2; public static final int COLUMN1 = (int)...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
2
by: Allen | last post by:
// 1. RPCMethodRegistry.h typedef int (*RPCMethod)(CRPCParaPacker& packer); class CRPCMethodEntry { public: int id; RPCMethod method; };
4
by: =?Utf-8?B?QWxleCBLLg==?= | last post by:
Hi all I need a simple program that allows me to check if an IP address is pingable. I am not going to send/receive anything to the remote host, just check if it is visible. Something like...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
5
by: Neo | last post by:
hi, I searched in the groups here but didnt find any post answering my question. My question is I need the address of the member function in the same class. Is there anyway to go about it other...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.