473,769 Members | 7,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

I'm getting the following compile-time error:

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

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

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 32207
shanknbake wrote:
I'm getting the following compile-time error:

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

Here is my getCount function declaration:
-----------------------------------------------------------------------------------
//person.h
class Person
{
....
public:
...
int getCount();
}
----------------------------------------------------------------------------------
//person.cpp
int Person::getCoun t()
{
return count;
}
----------------------------------------------------------------------------------
//test.cpp
int main()
{
...
for (int i = 0; i < Person::getCoun t(); i++)
Here you call getCount() static member of the Person class, but it is
not declared as such

persPtr[i]->print();
int total = Person::getCoun t();
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...@gma il.comwrote:
I'm getting the following compile-time error:

error C2352: 'Person::getCou nt' : 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::getCoun t()
{
return count;
}

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

// the static counter...
std::cout << "Person::getCou nt = " << Person::getCoun t();

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

/*
Person::getCoun t = 10
number of people = 10
*/



Apr 17 '07 #3
shanknbake wrote:
I'm getting the following compile-time error:

error C2352: 'Person::getCou nt' : 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...@gma il.comwrote:
I'm getting the following compile-time error:

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

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

---------------------------------------------------------------------------*-------
//person.cpp
int Person::getCoun t()
{
return count;}

---------------------------------------------------------------------------*-------
//test.cpp
int main()
{
...
for (int i = 0; i < Person::getCoun t(); i++)
persPtr[i]->print();
int total = Person::getCoun t();
...}

---------------------------------------------------------------------------*--------

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::getCoun t();
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.getCo unt();
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.goo glegroups.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.goo glegroups.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::getCoun t() 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::getCoun t() 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...@gma il.comwrote:
I'm getting the following compile-time error:
error C2352: 'Person::getCou nt' : illegal call of non-static member
function
Here is my getCount function declaration:
-----------------------------------------------------------------------------------
//person.h
class Person
{
....
public:
...
int getCount();}
----------------------------------------------------------------------------------
//person.cpp
int Person::getCoun t()
{
return count;}
----------------------------------------------------------------------------------
//test.cpp
int main()
{
...
for (int i = 0; i < Person::getCoun t(); i++)
persPtr[i]->print();
Where is persPtr declared? And what is its relationship with
Person::getCoun t()? A priori, what you need in the for loop is
something like:

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

If Person::getCoun t() 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::getCoun t();
...}
-----------------------------------------------------------------------------------
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 responsibilitie s in the total program, and who
manages it?

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

Apr 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
2955
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 here is that the graphic designer wants a pretty (non-webbable) font on the shaded nav tabs, so I'm reduced to using bitmaps. To make the 4-way rollovers ("current" and "hover" states) work I'm using the standard "rollerblind" technique with a...
14
42429
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 = Array(\, /, :, *, ?, ", <, >, |) If Me.TextBox Contains illegal Then MsgBox "You entered illegal characters. Please try again." Me.TextBox = Null
4
2814
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 info to another hardware interface. Since this is a non-critical piece, I simply had UNC Path to transfer the info as XML. But even at my initial testing of the application, I had bloopers
12
2378
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), the new class thread get as parameter reference to his father( CLASS who mad it (classA)), The new thread need to call methodes from his father like myfathe.methode(), This worked in VS 2003 but now in VS 2005 Beta throw an exception : An...
12
7198
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 illegal cross thread operation if posting a message. Is this a bug introduced in latest version of dotnet ? It is very legal to use postmessage because the message will execute in context of the thread the one was posted to. Same with...
0
1075
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 Expert provides the details at: http://www.ExpertHumor.com/
5
8009
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 syntax error at "int c =". The very similar: for (int c = getchar(); c != EOF; c = getchar()) {
4
4021
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. the sources compiled with gcc 4.1.2 crash with illegal instruction error: (running my application)
1
13054
by: Manish | last post by:
The code is as ... $folderlistfile = $path."xmlfile.xml"; /* <list> <details id="2"> <name>Books</name>
22
7271
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) {
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.