illegal call of non-static member function [why???]

S

shanknbake

I'm getting the following compile-time error:

error C2352: 'Person::getCount' : illegal call of non-static member
function

Here is my getCount function declaration:
-----------------------------------------------------------------------------------
//person.h
class Person
{
....
public:
...
int getCount();
}
----------------------------------------------------------------------------------
//person.cpp
int Person::getCount()
{
return count;
}
----------------------------------------------------------------------------------
//test.cpp
int main()
{
....
for (int i = 0; i < Person::getCount(); i++)
persPtr->print();
int total = Person::getCount();
....
}
-----------------------------------------------------------------------------------

Do you see something that I don't? Mind I'm a bit of a beginner with C+
+. I've spent far too long trying to figure this one out.

Thanks in advance for any help.
 
A

anon

shanknbake said:
I'm getting the following compile-time error:

error C2352: 'Person::getCount' : illegal call of non-static member
function

Here is my getCount function declaration:
-----------------------------------------------------------------------------------
//person.h
class Person
{
....
public:
...
int getCount();
}
----------------------------------------------------------------------------------
//person.cpp
int Person::getCount()
{
return count;
}
----------------------------------------------------------------------------------
//test.cpp
int main()
{
...
for (int i = 0; i < Person::getCount(); i++)

Here you call getCount() static member of the Person class, but it is
not declared as such

persPtr->print();
int total = Person::getCount();


Here as well
 
S

Salt_Peter

I'm getting the following compile-time error:

error C2352: 'Person::getCount' : illegal call of non-static member
function

Here is my getCount function declaration:
-----------------------------------------------------------------------------------
//person.h
class Person
{
....
public:
...
int getCount();}

Its a class declaration, semicolon missing

class Person { ... };

You've not shown nor explained the purpose of member count. What are
you counting?
The number of Persons staticly?
If so, your count variable should be static and only static member
functions can access static members.
Static variables also need to be initialized appropriately (not in
ctor).

Although i fail to see why Person should be tracking the count at all.
Specially since many container implentations will copy instead of
invoke a default ctor repeatedly.
Oops, that means you need to ++count on copy and also define a dtor
with --count to account for temporaries.
Not pretty.

#include <iostream>
#include <vector>

class Person
{
static int count;
public:
Person() { ++count; }
Person(const Person& copy) { ++count; }
~Person() { --count; }
static int getCount();
};

int Person::count; // static member initialization

// static member function
int Person::getCount()
{
return count;
}

int main()
{
std::vector< Person > people(10);

// the static counter...
std::cout << "Person::getCount = " << Person::getCount();

// the easy way...
std::cout << "\nnumber of people = " << people.size();
std::cout << std::endl;
}

/*
Person::getCount = 10
number of people = 10
*/
 
Z

Zeppe

shanknbake said:
I'm getting the following compile-time error:

error C2352: 'Person::getCount' : illegal call of non-static member
function

Do you see something that I don't? Mind I'm a bit of a beginner with C+
+. I've spent far too long trying to figure this one out.

in C++ static member functions are functions that you don't call on
specific class objects, but on the class itself. The difference is that,
having

class Foo
{
public:
void a();
static void b();
};

you can call a() on every object of class Foo. For example:

Foo pippo;
pippo.a();

b() being a static function, you can do like this:

Foo::b();

that is the syntax for a static call.

The static functions are useful to encompass properties or behaviours
that are not related to the specific instance of a class, but on the
class itself.

Regards,

Zeppe
 
X

xxs

I'm getting the following compile-time error:

error C2352: 'Person::getCount' : illegal call of non-static member
function

Here is my getCount function declaration:
---------------------------------------------------------------------------­--------
//person.h
class Person
{
....
public:
...
int getCount();}

---------------------------------------------------------------------------­-------
//person.cpp
int Person::getCount()
{
return count;}

---------------------------------------------------------------------------­-------
//test.cpp
int main()
{
...
for (int i = 0; i < Person::getCount(); i++)
persPtr->print();
int total = Person::getCount();
...}

---------------------------------------------------------------------------­--------

Do you see something that I don't? Mind I'm a bit of a beginner with C+
+. I've spent far too long trying to figure this one out.

Thanks in advance for any help.



int total = Person::getCount();
I think this is where it is wrong
As "getCount();" isn't a static function ,you can't use it as so.
It should be used as followed,I think.
Person pa;
......=pa.getCount();
you should rewrite the class
//person.h
class Person
{
....
public:
...
static int getCount();
}

I'm a beginner with C+ + too.
 
H

Howard

Salt_Peter said:
... and only static member
functions can access static members.

Eh? Since when is access to static members limited to only static member
functions?

It's the other way around... static member functions can only access static
member data, because there is no "this" pointer with which to access
specific instance data.

But any member function, static or not, can access static member data.

-Howard
 
V

Victor Bazarov

Howard said:
Eh? Since when is access to static members limited to only static
member functions?

It's the other way around... static member functions can only access
static member data, because there is no "this" pointer with which to
access specific instance data.

But any member function, static or not, can access static member data.

Nit pick: static member functions _can_ (and definitely _may_) access
non-static data _iff_ they have an instance of the class. Yes, there
is no 'this', so any non-static member name 'blah' should not just be
resolved as 'this->blah', but nothing would prevent accessing 'blah'
for a known 'object':

class a {
int blah;
static void foo(a& ra) {
ra.blah = 42; // access to non-static data member
}
};

V
 
S

shanknbake

I tried using 'static int getCount()' so that Public::getCount() would
work, but it give me problems with not being able to use the member
variable 'int count'. Take a look (I'll give my entire program here)
---------------------------------------------------------------------------------------------------------------------------------------------------------
http://rafb.net/p/Cwqix279.html

The error I got is listed at the top.

I'm still very VERY foggy on this whole "static member variable /
static function...etc..." stuff. I've never learned it before and so I
don't believe I need to use it on this program (or at least I
shouldn't have to). The driver file that you see was given by my
professor and is not my own so it is assumed to be correct and thus, I
need to code my classes and member functions accordingly.

BTW this is a simplified version of the program that I'm working on.
Oh and the purpose of count and getCount is to determine the number of
people that the program loops through. (no more than 3 for my
purposes).

thanks
 
A

anon

shanknbake said:
I tried using 'static int getCount()' so that Public::getCount() would
work, but it give me problems with not being able to use the member
variable 'int count'. Take a look (I'll give my entire program here)
---------------------------------------------------------------------------------------------------------------------------------------------------------
http://rafb.net/p/Cwqix279.html

The error I got is listed at the top.

I'm still very VERY foggy on this whole "static member variable /
static function...etc..." stuff. I've never learned it before and so I

To see about the static members and data, do the following:
1) open the browser
2) go to www.google.com
3) type "c++ static member" (without quotes)
4) pick up the link
It might be wrong using static members in your case

**after reading your code**
count needs to be static as well, and you need to lower it in the
Person's destructor
 
J

James Kanze

I'm getting the following compile-time error:
error C2352: 'Person::getCount' : illegal call of non-static member
function
Here is my getCount function declaration:
-----------------------------------------------------------------------------------
//person.h
class Person
{
....
public:
...
int getCount();}
----------------------------------------------------------------------------------
//person.cpp
int Person::getCount()
{
return count;}
----------------------------------------------------------------------------------
//test.cpp
int main()
{
...
for (int i = 0; i < Person::getCount(); i++)
persPtr->print();


Where is persPtr declared? And what is its relationship with
Person::getCount()? A priori, what you need in the for loop is
something like:

for ( int i = 0 ; i < numberOfElementsIn_persPtr ; ++ i ) {
persPtr[ i ]->print() ;

If Person::getCount() is supposed to track the number of
elements in persPtr, and those elements are of type Person*
(just a guess), it should be static. But it seems a very
strange organization; I would not expect a class Person to know
about the number of elements in some array unless it also
managed that array.
int total = Person::getCount();
...}
-----------------------------------------------------------------------------------
Do you see something that I don't?

Probably lots of things, but without seeing more, it's hard to
say what is relevant. Other's have addressed the issue of
static functions, in isolation, but I wouldn't be surprised if
the problem isn't at a higher level. What is persPtr: what is
its role and its responsibilities in the total program, and who
manages it?
 
A

anon

anon said:
**after reading your code**
count needs to be static as well, and you need to lower it in the
Person's destructor

Just a small add: using static data like that, you are risking a
possible Static Order Fiasco. Better take a look in the FAQ about static
members
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top