Solved

static functions

Posted on 2003-11-08
11
212 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

867 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now