Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can't access static member var from static method. Why?

Reply
Thread Tools

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

 
 
Markus Dehmann
Guest
Posts: n/a
 
      06-30-2004
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
 
Reply With Quote
 
 
 
 
JKop
Guest
Posts: n/a
 
      06-30-2004
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
 
Reply With Quote
 
 
 
 
Andreas Schmidt
Guest
Posts: n/a
 
      07-01-2004
On Wed, 30 Jun 2004 23:08:14 GMT, JKop <(E-Mail Removed)> 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;
}
}
 
Reply With Quote
 
Andreas Schmidt
Guest
Posts: n/a
 
      07-01-2004
On Wed, 30 Jun 2004 21:44:59 -0400, Andreas Schmidt <(E-Mail Removed)>
wrote:

> On Wed, 30 Jun 2004 23:08:14 GMT, JKop <(E-Mail Removed)> 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.


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      07-01-2004
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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      07-01-2004
Andreas Schmidt wrote:
> On Wed, 30 Jun 2004 23:08:14 GMT, JKop <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 10 06-20-2005 01:02 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 0 06-20-2005 10:54 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM



Advertisments