|
-
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
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
|