Thread: non-static member error

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    non-static member error

    Hi, i have made the .h and .cpp file for an assignment, but upon compiling i get a "illegal call of non-static member function". Why is this happening Thanks in advance for the help ......ALso another question, do i need a copy constructor in this case since 2 counters will be running in the program????

    Here is the main.cpp file http://www.cs.uh.edu/~svenkat/lib/as...N1/assign1.cpp .

    .h file
    Code:
    class ModuloCounter
    {
    public:
    	ModuloCounter(int );
    	~ModuloCounter();
    	void increment (void);
    	void decrement (void);
    	int getNumberofModuloCounters(void)const;
    	int getCounterLimit(int)const;
    	int getCounterValue(void)const;
    
    private:
    	int modLim;
    	int currVal;
    	int modCount;
    };
    the .cpp file for the .h
    Code:
    #include "ModuloCounter.h"
    
    ModuloCounter::ModuloCounter(int set)
    {
    	if(modCount==1)modCount++;
    	modLim=set;
    	
    
    }
    
    ModuloCounter::~ModuloCounter()
    {
    	modCount--;
    }
    
    void ModuloCounter::increment(void)
    {
    	if(currVal==modLim)currVal=1;
    	else currVal++;
    }
    
    void ModuloCounter::decrement(void)
    {
    	if(currVal==1)currVal=modLim;
    	else currVall--;
    }
    
    int getNumberofModuloCounters(void)const
    {
    	return modCount;
    }
    
    int getCounterLimit(int)const
    {
    	return modLim;
    }
    
    int getCounterValue(void)const
    {
    	return currVall;
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Okay, when you have a static function/variable, there is one copy of it which is shared, and therefore, if it belongs to an object, the object does not have to be instantiated for it to be called/used.

    In the main.cpp file, there are a couple lines in the last teo functions:
    Code:
    cout << "Number of Counters while in printCounterValueAndCount_2: " <<
      ModuloCounter::getNumberofModuloCounters() << endl;
    Notice that the function is not being called with the instance of an object. This means, that it is expected to be static. This also means that the variables within the class that it accesses also have to be static (since there is no concrete object associated with them).

    Hope this makes some sense.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    so I have to change some of the variables to static int to fix it?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Well, that, and I just noticed, you left off some 'ModuloCounter::'s in your .cpp file.

    Anyways, yeah, make the function being called without an object (in this case) static, and the variable(s) it accesses (since there is no object associated with the function, there can't be with the data it accesses). Also, initialize your counter somewhere (top of .cpp file, probably). This is the one time that you can 'access' private data outside a class.

    i.e.
    Code:
    // .h
    class my_class
    {
       static int x;
    };
    
    // .cpp
    int my_class::x = 0;
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    the main.cpp file has been created by the proff , so i cant touch that (instructions say that)

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You don't need to edit the main.cpp file.

    My post was ambiguous, I admit, but intentionally so. Consider what is being accessed in the main.cpp file without an object.

    And the initialization would best go in your ModuloCounter.cpp file. Anyways, there are two things I see which need to be made static.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    even after making those 2 static im getting the same error

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by noob2c
    even after making those 2 static im getting the same error
    So create an instance of the object and be done with it. Either that, or make whatever you're calling static.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Originally posted by noob2c
    even after making those 2 static im getting the same error
    Which two did you make static?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    these two

    Code:
    int currVal;
    int modCount

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Of those, only modCount need be static. The other static 'thing' is a function.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The function you are calling has to be static.
    Code:
    class Foo
    {
        public:
            static void myFun( void )
            { cout << "I can be called without an instance of this class!" << endl; }
            void fun my2ndFun( void )
            { cout << "I can not." << endl; }
    };
    
    ...
    
    Foo::myFun( );


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    made these changes, still nothing
    asign.cpp with all instructions is at http://www.cs.uh.edu/~svenkat/lib/as...r2003/ASSIGN1/

    ModuloCounter.cpp file
    Code:
    #include "ModuloCounter.h"
    
    ModuloCounter::ModuloCounter(int set)
    {
    	if(modCount==1)modCount++;
    	modLim=set;
    	
    
    }
    
    ModuloCounter::~ModuloCounter()
    {
    	modCount--;
    }
    
    void ModuloCounter::increment(void)
    {
    	if(currVal==modLim)currVal=1;
    	else currVal++;
    }
    
    void ModuloCounter::decrement(void)
    {
    	if(currVal==1)currVal=modLim;
    	else currVal--;
    }
    
    static int ModuloCounter:: getNumberofModuloCounters(void)
    {
    	return modCount;
    }
    
    int ModuloCounter:: getCounterLimit(int)const
    {
    	return modLim;
    }
    
    int ModuloCounter:: getCounterValue(void)const
    {
    	return currVal;
    }
    ModuloCounter.h
    Code:
    class ModuloCounter
    {
    public:
    	ModuloCounter(int );
    	~ModuloCounter();
    	void increment (void);
    	void decrement (void);
    	static int getNumberofModuloCounters(void);
    	int getCounterLimit(int)const;
    	int getCounterValue(void)const;
    
    private:
    	int modLim;
    	int currVal;
    	static int modCount;
    };
    Last edited by noob2c; 07-12-2003 at 08:15 AM.

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    after finding out that I made some spelling errors, it compiles fine but on build gives me this "getNumberofModuloCounters' : 'static' should not be used on member functions defined at file scope"....how to fix this

  15. #15
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Should be easy enough to fix. Only the declaration needs the keytword 'static'. The definition doesn't:

    Code:
    class foo
    {
       static void bar();
    };
    
    void foo::bar()
    {
    }
    This should get rid of that error.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  5. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM