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

L'il help with static member please?

Hi All,

I have a class like this:
//================================
class foo
{
public:

static foo* foo_ptr;

void bar();
}
//================================
Then I have a member function like this:
//================================
void foo::bar()
{
foo_ptr = NULL;
}
//================================
The linker is telling me:
//================================
unresolved external symbol "public: static class foo * foo::foo_ptr" (?
foo_ptr@foo@@2PAV1@A)
//================================

Any ideas on what I'm doing wrong?
Everything is fine if I move the foo_ptr out of the class and below the
class definition in the header file, but I'd like it to be a member.
Thanks,

-L
Jul 22 '05 #1
6 1967
Lilia wrote:

I have a class like this:
//================================
class foo
{
public:
static foo* foo_ptr;
void bar();
}
Don't forget the semicolon following the class definition.
//================================

Then I have a member function like this:
//================================
void foo::bar()
{
foo_ptr = NULL;
}
//================================

The linker is telling me:
//================================
unresolved external symbol "public: static class foo * foo::foo_ptr" (?
foo_ptr@foo@@2PAV1@A)
//================================


This is addressed in the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-10.10

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 22 '05 #2
Lilia wrote in news:PK***********@fe03.usenetserver.com:
//================================
class foo
{
public:

static foo* foo_ptr;

void bar();
}
//================================

The above is only a declaration, you also need a defenition:

foo * foo::foo_ptr = NULL;

Your linker should now be able to find:
unresolved external symbol "public: static class foo * foo::foo_ptr" (?
foo_ptr@foo@@2PAV1@A)


HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
Wow, and just when I was getting pretty cocky about my C++ knowledge. :)

It'll be interesting to pull out the 'ol Stroustrup and figure out why
the 2-step (why can't it just be assumed to be a definition as well, is
there a time when you wouldn't do both steps?).
Thanks for the help.
-Lilia

PS. Thanks to Russell for the http://www.parashift.com/c++-faq-lite/
link

Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in
news:Xn*********************************@195.129.1 10.205:
Lilia wrote in news:PK***********@fe03.usenetserver.com:
//================================
class foo
{
public:

static foo* foo_ptr;

void bar();
}
//================================


The above is only a declaration, you also need a defenition:

foo * foo::foo_ptr = NULL;

Your linker should now be able to find:
unresolved external symbol "public: static class foo * foo::foo_ptr" (? foo_ptr@foo@@2PAV1@A)


HTH.

Rob.

Jul 22 '05 #4
Lilia wrote in news:UD********************@fe09.usenetserver.com:
Wow, and just when I was getting pretty cocky about my C++ knowledge. :)

It'll be interesting to pull out the 'ol Stroustrup and figure out why
the 2-step (why can't it just be assumed to be a definition as well, is
there a time when you wouldn't do both steps?).


Well if it was a defenition in a header (as such declaration's often
are) then included by 2 source files, you would get a duplication
problem. Also where does the initializer go and which source file
compiles the initializer ?

// header
#include <cmath>

struct X
{
static double sqrt2;
};

//source file
double X::sqrt2 = std::sqrt( 2.0 );

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

"Lilia" <LL@x.x> wrote in message
news:UD********************@fe09.usenetserver.com. ..
Wow, and just when I was getting pretty cocky about my C++ knowledge. :)

It'll be interesting to pull out the 'ol Stroustrup and figure out why
the 2-step (why can't it just be assumed to be a definition as well, is
there a time when you wouldn't do both steps?).

I view it this way.
Static objects are specific to a class and shared by all objects of the class.
Whenever you instantiate objects of a class each object gets it's due of member
variables.
But what about the static instance?
It can't get defined with each object, so it's your duty now to do so separately
in an implementation file.

-Sharad
Jul 22 '05 #6
Ahh,
Makes sense.

Too bad it can't just be assumed that the first time it's seen by the
compiler it's initialized and everything else is just redundant.

-Lilia

"Sharad Kala" <no*****************@yahoo.com> wrote in
news:bv*************@ID-221354.news.uni-berlin.de:

"Lilia" <LL@x.x> wrote in message
news:UD********************@fe09.usenetserver.com. ..
Wow, and just when I was getting pretty cocky about my C++ knowledge.
:)

It'll be interesting to pull out the 'ol Stroustrup and figure out
why the 2-step (why can't it just be assumed to be a definition as
well, is there a time when you wouldn't do both steps?).

I view it this way.
Static objects are specific to a class and shared by all objects of
the class. Whenever you instantiate objects of a class each object
gets it's due of member variables.
But what about the static instance?
It can't get defined with each object, so it's your duty now to do so
separately in an implementation file.

-Sharad

Jul 22 '05 #7

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

Similar topics

5
by: Surya Kiran | last post by:
Hi all, I've 2 classes class A, and class B. and i want to use list of type B in class A. like this list<B> typeB ; till now its fine. But i want to make it a static member. like static...
5
by: William Payne | last post by:
Hello, consider the following two classes (parent and child): #ifndef SINGLETON_HPP #define SINGLETON_HPP #include <cstddef> /* NULL */ template <typename T> class Singleton {
4
by: fred | last post by:
Hi, can someone please explain to me how a static member of a class works. For example if I have: public Class1 public static Class2 myClass2 = new Class2; public static Int16 thisValue = 200;...
4
by: Leon Lambert | last post by:
I would appreciate it if someone could help me understand NaN handling with respect to conditionals in IL code. I am playing with a small IL interpreter and having a little problem with it....
2
by: Gabrielle A. Grün | last post by:
Hi All, Does anyone know a way around the illegal reference of a non-static member? Thanks. iNHERITANCE HIERACY
8
by: Daz | last post by:
Hi all! This question may hopefully spark a little debate, but my problem is this: I am sure it's not just me who struggles to think up names for variables. Obviously, thinking up a name...
4
by: B. Williams | last post by:
I have written this program for an assignment that requires a static member function to set a static data member, but I can't figure out how to get it to change the value once set. Would someone...
12
by: StephQ | last post by:
I face the following problem. I wrote a poor's man plotting function: computePlot. This function append some x-values belonging to step and the correseponding f( x ) (for a given const member...
6
by: VSP | last post by:
Hello, I am just implementing singleton pattern in various ways. In one implementation I created a static member and returning that static member in the getInstance() function. I have made...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.