Solved

static functions

Posted on 2003-11-08
11
220 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
Technology Partners: 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

Technology Partners: 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!

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. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

710 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