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.
that? Is the address assigned at load time?
Thanks.
cppsks said:Taking the address of a static const resulted in a unresolved symbol.
Why is that? Is the address assigned at load time?
cppsks said:Taking the address of a static const resulted in a unresolved symbol. Why is
that? Is the address assigned at load time?
cppsks said:Taking the address of a static const resulted in a unresolved symbol.
Why is that?
Is the address assigned at load time?
#include <iostream>cat main.cc
a = 13g++ -Wall -ansi -pedantic -o main main.cc
./main
cppsks said:Taking the address of a static const resulted in a unresolved symbol.
Why is that?
Is the address assigned at load time?
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?
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:rintMe(&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
Thomas said:You need to specify an instance of the variable: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:rintMe(&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
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:rintMe(&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.
E. Robert Tisdale said:Thomas said:You need to specify an instance of the variable: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:rintMe(&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
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++.
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:rintMe(&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.
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.