473,320 Members | 1,930 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,320 software developers and data experts.

Can't access static member var from static method. Why?

My ("static") California class has a list of cities, and a static
member function returns true if a certain city is contained:

California::contains("San Francisco"); // true

I implemented it like below, but it does not compile. It gives me some
obscure linker error (see below). What's wrong with my code?

#include <iostream>
#include <map>

class California{
private:
static std::map<std::string, int> cities;
public:
California(){
cities.insert(std::make_pair("San Francisco", 1));
}
static bool contains(std::string city){
return cities.find(city) != cities.end();
}
};

int main(){
if(California::contains("San Francisco")){
std::cout << "SF is in CA!" << std::endl;
}
}

bash-2.05b$ g++ california.cpp
/tmp/ccduZZS6.o(.gnu.linkonce.t._ZN10California8contain sESs+0x11): In
function `California::contains(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `California::cities'
/tmp/ccduZZS6.o(.gnu.linkonce.t._ZN10California8contain sESs+0x2a): In
function `California::contains(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >)':
: undefined reference to `California::cities'
collect2: ld returned 1 exit status
Jul 22 '05 #1
5 1565
Markus Dehmann posted:

#include <iostream>
#include <map>

class California{
private:
static std::map<std::string, int> cities;
public:
California(){
cities.insert(std::make_pair("San Francisco", 1));
}
static bool contains(std::string city){
return cities.find(city) != cities.end();
}
};

std:map<std::string,int> California::cities;


int main(){
if(California::contains("San Francisco")){
std::cout << "SF is in CA!" << std::endl;
}
}


Now all is well.
-JKop
Jul 22 '05 #2
On Wed, 30 Jun 2004 23:08:14 GMT, JKop <NU**@NULL.NULL> wrote:
Markus Dehmann posted:
class California{
private:
static std::map<std::string, int> cities;
public:
California(){
cities.insert(std::make_pair("San Francisco", 1));
}
static bool contains(std::string city){
return cities.find(city) != cities.end();
}
};


std:map<std::string,int> California::cities;
int main(){
if(California::contains("San Francisco")){
std::cout << "SF is in CA!" << std::endl;
}
}


Now all is well.


You mean, now it compiles. But it does not work as intended: The map is
empty. So, the constructor is actually never called, which actually makes
sense, of course, since I never construct the object. Java has a static{}
block for static initialization. What can I use in C++?

And, I don't want that users of my California class have to declare the
map California::cities. They don't have anything to do with my internal
data structure.

This is the sad output now (code see below):
bash-2.05b$ g++ california.cpp
bash-2.05b$ ./a.out
Number of cities: 0
SF is NOT in CA.

How can I make it right?
Thanks!
#include <iostream>
#include <map>

class California{
private:
static std::map<std::string, int> cities;
public:
California(){
std::cerr << "Constructor called" << std::endl;
cities.insert(std::make_pair("San Francisco", 1));
}
static bool contains(std::string city){
return cities.find(city) != cities.end();
}
static int getSize(){
return cities.size();
}
};

std::map<std::string,int> California::cities;

int main(){
std::cout << "Number of cities: " << California::getSize() <<
std::endl;
if(California::contains("San Francisco")){
std::cout << "SF is in CA!" << std::endl;
}else{
std::cout << "SF is NOT in CA." << std::endl;
}
}
Jul 22 '05 #3
On Wed, 30 Jun 2004 21:44:59 -0400, Andreas Schmidt <sc**********@gmx.de>
wrote:
On Wed, 30 Jun 2004 23:08:14 GMT, JKop <NU**@NULL.NULL> wrote:
[class]
std:map<std::string,int> California::cities;
[int main]

Now all is well.


You mean, now it compiles. But it does not work as intended: The map is
empty. So, the constructor is actually never called, which actually
makes sense, of course, since I never construct the object. Java has a
static{} block for static initialization. What can I use in C++?

And, I don't want that users of my California class have to declare the
map California::cities. They don't have anything to do with my internal
data structure.

This is the sad output now (code see below):
bash-2.05b$ g++ california.cpp
bash-2.05b$ ./a.out
Number of cities: 0
SF is NOT in CA.

How can I make it right?
Thanks!
#include <iostream>
#include <map>

class California{
private:
static std::map<std::string, int> cities;
public:
California(){
std::cerr << "Constructor called" << std::endl;
cities.insert(std::make_pair("San Francisco", 1));
}
static bool contains(std::string city){
return cities.find(city) != cities.end();
}
static int getSize(){
return cities.size();
}
};

std::map<std::string,int> California::cities;

int main(){
std::cout << "Number of cities: " << California::getSize() <<
std::endl;
if(California::contains("San Francisco")){
std::cout << "SF is in CA!" << std::endl;
}else{
std::cout << "SF is NOT in CA." << std::endl;
}
}


Now I think a singleton might be the best way to do this.
Jul 22 '05 #4
Andreas Schmidt posted:
Java has a static{} block for static initialization. What can I use
in C++?


Just work with it like any other global variable:

std::map<std::string,int> California::cities( ....... );
-JKop
Jul 22 '05 #5
Andreas Schmidt wrote:
On Wed, 30 Jun 2004 23:08:14 GMT, JKop <NU**@NULL.NULL> wrote:
Markus Dehmann posted:
class California{
private:
static std::map<std::string, int> cities;
public:
California(){
cities.insert(std::make_pair("San Francisco", 1));
}
static bool contains(std::string city){
return cities.find(city) != cities.end();
}
};

std:map<std::string,int> California::cities;
int main(){
if(California::contains("San Francisco")){
std::cout << "SF is in CA!" << std::endl;
}
}

Now all is well.

You mean, now it compiles. But it does not work as intended: The map is
empty. So, the constructor is actually never called, which actually
makes sense, of course, since I never construct the object. Java has a
static{} block for static initialization. What can I use in C++?

And, I don't want that users of my California class have to declare the
map California::cities. They don't have anything to do with my internal
data structure.

This is the sad output now (code see below):
bash-2.05b$ g++ california.cpp
bash-2.05b$ ./a.out
Number of cities: 0
SF is NOT in CA.

How can I make it right?
Thanks!
#include <iostream>
#include <map>

class California{
private:
static std::map<std::string, int> cities;
public:
California(){
std::cerr << "Constructor called" << std::endl;
cities.insert(std::make_pair("San Francisco", 1));
}
static bool contains(std::string city){
return cities.find(city) != cities.end();
}
static int getSize(){
return cities.size();
}
};

std::map<std::string,int> California::cities;

int main(){
std::cout << "Number of cities: " << California::getSize() <<
std::endl;
if(California::contains("San Francisco")){
std::cout << "SF is in CA!" << std::endl;
}else{
std::cout << "SF is NOT in CA." << std::endl;
}
}


Where did you ever create an object of class California? The only place
that something gets added to California::cities is in the constructor
for California. Since you never construct a California object, it's
perfectly correct (as written) for California::cities to be empty.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
3
by: Sushil Srivastava | last post by:
Hi Guys, Would you be able to help me using C# GUI (with user interface component) in my MFC application. I have used managed extension, COM-interops, etc but problem is this C# component has...
5
by: TomislaW | last post by:
What is the purpose or difference between private static and private method in non-static class?
5
by: Just Me | last post by:
Given a button name Btn_5 and Index=5 I want to do something like dim zz as string = Btn_??Index??.Text or given an array of buttons, do:
9
by: sonu | last post by:
Hi All, Pls clarify me what is the difference between static member and static method in c. pls some one reply me with Example.
5
by: Jon E. Scott | last post by:
I'm a little confused with "static" methods and how to access other unstatic methods. I'm a little new to C#. I'm testing a callback routine within a DLL and the callback function returns a...
3
by: dolphin | last post by:
Hello everyone! Can a static member function access non-static member? I think it is illegal.Is it right?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.