473,383 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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

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
10 32168
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
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
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
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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: dingbat | last post by:
I'm writing a "tabbed folder" nav bar. Site standards are graphical prettiness, CSS throughout, valid code, but accesibility is ignored where it conflicts with prettiness. The particular issue...
14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
4
by: Logu | last post by:
I have an interesting issue. rather I should call this as confusing issue. :-) I have a small proto in my project, which should act as a server, which essentially requests info and passes the...
12
by: [Yosi] | last post by:
What I should do to return back the permissin I had in VS2003 , that allowd me to access public methode of one Class from other Thread. I have Class A(FORM) which create new thread (new class B),...
12
by: Wilfried Mestdagh | last post by:
Hi, Using P/Invoke I use PostMessage to send a message from one thread to another. I'm pretty sure this has already worked in dotnet. but I've upgraded version 2 few day ago. Now I have an...
0
by: Expert Humor | last post by:
The way our politicians rushed to talk about illegal immigration this week, maybe they thought it came with a free lunch. They won't do anything about it, but it will get them some money. The...
5
by: Roy Smith | last post by:
The following code appears to be illegal: while ((int c = getchar()) != EOF) { putchar (c); } I tried it on two different compilers (Sun workshop and gcc), and both give some variation on...
4
by: Mathias Waack | last post by:
Hi, I've embedded python into a legacy application. It works - most of the time. In some special situations the app crashes executing the "import random". There are two different situations: ...
1
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
22
by: smarty | last post by:
this code gives an "illegal seek error" on close() call what is this error and when does it come? main() { int fd,num; char buf; fd = open ("123.c",O_RDWR); if(fd!=-1) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.