Solved

static functions

Posted on 2003-11-08
11
209 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
Comment Utility
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
Comment Utility
I do have () in my implementations
but it doesn't work
0
 
LVL 9

Accepted Solution

by:
tinchos earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
try to cal it as A::D() since it is static
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 5

Expert Comment

by:migoEX
Comment Utility
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
Comment Utility
what is the error you gor
0
 
LVL 10

Expert Comment

by:Sys_Prog
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

772 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

10 Experts available now in Live!

Get 1:1 Help Now