By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i]->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.

Apr 17 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
shanknbake wrote:
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[i]->print();
int total = Person::getCount();
Here as well
...
}
-----------------------------------------------------------------------------------

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.
Apr 17 '07 #2

P: n/a
On Apr 17, 5:17 am, shanknbake <firedhi...@gmail.comwrote:
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
*/



Apr 17 '07 #3

P: n/a
shanknbake wrote:
I'm getting the following compile-time error:

error C2352: 'Person::getCount' : illegal call of non-static member
function
[snip]
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
Apr 17 '07 #4

P: n/a
xxs
On Apr 17, 5:17 pm, shanknbake <firedhi...@gmail.comwrote:
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[i]->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.

Apr 17 '07 #5

P: n/a

"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
>
... 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
Apr 17 '07 #6

P: n/a
Howard wrote:
"Salt_Peter" <pj*****@yahoo.comwrote in message
news:11**********************@d57g2000hsg.googlegr oups.com...
>>
... 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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 17 '07 #7

P: n/a
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

Apr 17 '07 #8

P: n/a
shanknbake wrote:
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

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
Apr 18 '07 #9

P: n/a
On Apr 17, 11:17 am, shanknbake <firedhi...@gmail.comwrote:
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[i]->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?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 18 '07 #10

P: n/a
anon wrote:
shanknbake wrote:
>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

**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
Apr 18 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.