CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Error Using 'this' pointer in Static Members Function

    Greeting Codeguru world.
    I am trying to use 'this' pointer but i am confused why 'this' pointer is not available for static member functions.

    Any suggestion with correction ?

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    using namespace std;
    const int MAX = 20;
    const int MAXPTR = 100;
    
    class name
    {
        private :
            char fname[MAX], mname[MAX], lname[MAX];
            unsigned long number;
            static int n;
            static name* nptr[];
        public :
            virtual void getData()
            {
                cout << "\nEnter Employee's First Name : "; cin >> fname;
                cout << "\nEnter Employee's Middle Name : "; cin >> mname;
                cout << "\nEnter Employee's Last Name : "; cin >> lname;
                cout << "\nEnter Employee's Number : "; cin >> number;
            }
            virtual void showData()
            {
                cout << "\nEmployee's Full Name : " << fname << " " << mname << " " << lname << endl;
                cout << "\nEmployee's Number : " << number << endl;
            }
            static void writeIn();
            static void add_Data();
            static void show_Data();
            static void readOut();
    };
    
    int name::n;
    name* name::nptr[MAXPTR];
    
    void name::add_Data() {nptr[n++]->getData();}
    void name::show_Data()
    {
        for(int j=0;j<n;j++)
            nptr[j]->show_Data();
    }
    
    void name::writeIn()
    {
        cout << "\nWritting " << n << " Names\n";
        ofstream outfile;
        outfile.open("NAME.DAT",ios::binary | ios::app);
        if(!outfile){cerr <<"\nCan't Open File"; return;}
        outfile.write((char*)this, sizeof(*this));
    }
    
    void name::readOut()
    {
        cout << "\nReading Name " << n;
        ifstream infile;
        infile.open("NAME.DAT",ios::binary|ios::app);
        if(!infile){cerr << "\nCan't Open file to read";return;}
        while(!infile.eof()) {infile.read((char*)this,sizeof(*this));}
    }
    
    int main()
    {
        char ch;
        while(true)
        {
            cout    << "\n*******Choice Menu*******\n"
                    << "'a' for adding names to buffer\n"
                    << "'r' for reading names from file\n"
                    << "'w' for writting names to file\n"
                    << "'x' for Exit\n"
                    << "Enter Selection : "; cin >> ch;
            switch(ch)
            {
                case 'a' : name::add_Data(); break;
                case 'r' : name::readOut(); break;
                case 'w' : name::writeIn(); break;
                case 'x' : exit(0);
                default: cout <<"\nUnknown Key";
            }
        }
        return 0;
    }
    I am using GNU GCC Compiler via Code::Block

    Error : 'this' is unavailable for static member functions

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Error Using 'this' pointer in Static Members Function

    Quote Originally Posted by basanta View Post
    I am trying to use 'this' pointer but i am confused why 'this' pointer is not available for static member functions.
    It's because static members are associated with the class as a whole whereas non-static members are associated with the individual objects of the class.

    And since only objects have a this-pointer they're available in non-static members only.
    Last edited by nuzzle; September 4th, 2012 at 02:49 AM.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error Using 'this' pointer in Static Members Function

    Quote Originally Posted by basanta
    i am confused why 'this' pointer is not available for static member functions.
    Because a static member function does not have a current ("this") object. In fact, you can call a static member function even when no objects of the class exist.

    It appears that your static member functions are supposed to interact with the static member named nptr, so you should be doing that instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2012
    Location
    Kathmandu
    Posts
    31

    Re: Error Using 'this' pointer in Static Members Function

    Quote Originally Posted by laserlight View Post
    Because a static member function does not have a current ("this") object. In fact, you can call a static member function even when no objects of the class exist.

    It appears that your static member functions are supposed to interact with the static member named nptr, so you should be doing that instead.
    So, it will solve if i remove static to member function writeIn() and readOut() functions ? But, it supposed to create error in main() function..

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Error Using 'this' pointer in Static Members Function

    Quote Originally Posted by basanta
    So, it will solve if i remove static to member function writeIn() and readOut() functions ?
    No, since they don't look like good candidates for non-static member functions to me.

    Rather, don't use this in those functions. You don't need to, anyway.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured