Solved

static functions

Posted on 2003-11-08
11
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

617 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