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

Discussion in 'C++' started by shanknbake, Apr 17, 2007.

  1. shanknbake

    shanknbake Guest

    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.
     
    shanknbake, Apr 17, 2007
    #1
    1. Advertising

  2. shanknbake

    anon Guest

    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->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.
    >
     
    anon, Apr 17, 2007
    #2
    1. Advertising

  3. shanknbake

    Salt_Peter Guest

    On Apr 17, 5:17 am, 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();}


    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
    */
     
    Salt_Peter, Apr 17, 2007
    #3
  4. shanknbake

    Zeppe Guest

    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
     
    Zeppe, Apr 17, 2007
    #4
  5. shanknbake

    xxs Guest

    On Apr 17, 5:17 pm, 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++)
    > 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.
     
    xxs, Apr 17, 2007
    #5
  6. shanknbake

    Howard Guest

    "Salt_Peter" <> wrote in message
    news:...
    >
    > ... 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
     
    Howard, Apr 17, 2007
    #6
  7. Howard wrote:
    > "Salt_Peter" <> wrote in message
    > news:...
    >>
    >> ... 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
     
    Victor Bazarov, Apr 17, 2007
    #7
  8. shanknbake

    shanknbake Guest

    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
     
    shanknbake, Apr 17, 2007
    #8
  9. shanknbake

    anon Guest

    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
    >
     
    anon, Apr 18, 2007
    #9
  10. shanknbake

    James Kanze Guest

    On Apr 17, 11:17 am, 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++)
    > 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?

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Apr 18, 2007
    #10
  11. shanknbake

    anon Guest

    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
     
    anon, Apr 18, 2007
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,002
    Smokey Grindel
    Dec 2, 2006
  2. Ook
    Replies:
    7
    Views:
    901
    David White
    Oct 20, 2005
  3. aling
    Replies:
    6
    Views:
    484
    Xiaobin.Huang
    Oct 30, 2005
  4. Gabrielle  A. Grün

    illegal reference to non-static member

    Gabrielle A. Grün, Jan 7, 2006, in forum: C++
    Replies:
    2
    Views:
    6,194
    David Harmon
    Jan 7, 2006
  5. dolphin
    Replies:
    3
    Views:
    1,350
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page