Solved

static functions

Posted on 2003-11-08
11
214 Views
Last Modified: 2010-04-02
I have a Comparator class
for example:

class A {
public:
static bool B { return ...; }
static bool C { return ...; }
static bool D { return ...;}
};

It works fine, but if I try to call D() in B(), it gives error

class A {
public:
static bool B { return ...; }
static bool C {
.....
D();        //calling D() here
return ....;
}
static bool D { return ...;}
};

I am using g++, it gives like
Controller.o ..... undefined reference to .....
collect2: ld returned 1 exit status

I call this class in Controller.cpp

does anyone know how to solve it, or static function can't call another function of its class?
I have tried to make D() a bool function instead of static bool, it gives same error

0
Comment
Question by:nofearse
11 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9708368
Hi nofearse,

You're missing the () ni the declaration of the functions

try with.....

class A {
public:
static bool B() { return true; }
static bool C() {
D();        //calling D() here
return false;
}
static bool D() { return false;}
};

I'm using MSVC6 and it works OK

Note: the return true and return false are just so that it could compile


Cheers!

Tincho
0
 

Author Comment

by:nofearse
ID: 9708371
I do have () in my implementations
but it doesn't work
0
 
LVL 9

Accepted Solution

by:
tinchos earned 500 total points
ID: 9708382
try if the code I posted works or not...........

That piece of code i posted was compiling for me...........

If that piece of code works for you, then you just have to see what are the differences between that and your code (I've worked with what was posted).....

If not, then the problem will be in the compiler, as in mine is working....................

Hope this helps

Tincho

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 23

Expert Comment

by:brettmjohnson
ID: 9708633
You have a problem with forward declarations.  Either implement of D() before B()
or prototype D() before B().

class A {
public:
static bool D() { return ...;}
static bool B() { .... ; D();  return ....; }
static bool C() { return ...; }
};

- OR -

class A {
public:
static bool D();
static bool B() { .... ; D();  return ....; }
static bool C() { return ...; }
static bool D() { return ...;}
};
0
 
LVL 5

Expert Comment

by:waelothman
ID: 9708679
try to cal it as A::D() since it is static
0
 
LVL 5

Expert Comment

by:migoEX
ID: 9709277
For functions in same class there's no importance for the order they are defined. You also don't need to call it A::D(), but only D().

tinchos is right, and it works just in the way as he posted it.
0
 
LVL 5

Expert Comment

by:waelothman
ID: 9710856
what is the error you gor
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 9712484
Hi nofearse,

I am also using a GNU compiler and it works fine for me.

I think the error is some other part of your code. You can post some more code and the actual error message, so that we can dig into it

--Amit
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10248815
This question has been classified as abandoned.  I will make a recommendation to the moderators on its resolution in approximately one week.  I would appreciate any comments by the experts that would help me in making a recommendation.

It is assumed that any participant not responding to this request is no longer interested in its final deposition.

If the asker does not know how to close the question, the options are here:
http://www.experts-exchange.com/help.jsp#hs5

Tinchos
EE Cleanup Volunteer
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10286293
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: tinchos {http:#9708382}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question