-
September 4th, 2012, 02:36 AM
#1
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
-
September 4th, 2012, 02:47 AM
#2
Re: Error Using 'this' pointer in Static Members Function
Originally Posted by basanta
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.
-
September 4th, 2012, 02:49 AM
#3
Re: Error Using 'this' pointer in Static Members Function
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.
-
September 4th, 2012, 03:38 AM
#4
Re: Error Using 'this' pointer in Static Members Function
Originally Posted by laserlight
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..
-
September 4th, 2012, 03:43 AM
#5
Re: Error Using 'this' pointer in Static Members Function
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|