error: missing storage-class or type specifiers --- why is it comming

i am using vc++ 6.0 for learning c++. i have made a program which give me the following error

error C2501: 'TotalStudents' : missing storage-class or type specifiers

Here 'TotalStudents' is a static member. Actually i m using binary stream classes to read/write data to/from a binary file.

here is all the code. sorry but i could not format it. any help would be greatly appreciated.



#include<iostream>
#include<fstream>
#include<conio.h>

#include<string>

using namespace std;


/*
This programe uses file stream to save student objects --- GIVES ERROR
-------------------------------------------------------------
error C2501: 'TotalStudents' : missing storage-class or type specifiers

Please help me resolve this error
*/

class Person
{
protected:
char name[20];
};

class Student:public Person
{
private:
char regnum[10];
char programme[8];
int semester;
float cgpa;

public:
void AddNewStudent()
{
system("cls");
ofstream objFile;

cout<<endl<<"\n\t\t\t\tEnter student name: ";
cin>>name;

cout<<endl<<"\n\t\t\t\tEnter Regnumber: ";
cin>>regnum;

cout<<endl<<"\n\t\t\t\tEnter programme: ";
cin>>programme;

cout<<endl<<"\n\t\t\t\tEnter semester: ";
cin>>semester;

cout<<endl<<"\n\t\t\t\tcgpa: ";
cin>>cgpa;

//open the file to save this student object
objFile.open("Students.dat",ios::out | ios::app | ios::binary);
objFile.write((char*)this,sizeof(*this));
}


void ShowStudent()
{
cout<<"\nStudent "<<endl;
cout<<"----------"<<endl;
cout<<"\tName: "<<name<<endl;
cout<<"\tReg#: "<<regnum<<endl;
cout<<"\tPrograme: "<<programme<<endl;
cout<<"\tSemester: "<<semester<<endl;
cout<<"\tCGPA: "<<cgpa<<endl;
}
};


class StudentOP
{
private:
static enum {MAX=50};
static Student objP[MAX];

public:
static int TotalStudents;

static void CreateStudentDB()
{

LoadStudentObjects(false);
do
{
Student s;
s.AddNewStudent();
objP[TotalStudents] = s;
TotalStudents++;

cout<<"Add new student(y/n): ";
}while(getch() == 'y');

//TotalStudents = i;
}

//get total no of students by dividing total file size by a student object size
static int GetTotalStudents()
{
ifstream objFile;
objFile.open("Students.dat",ios::binary);
objFile.seekg(0,ios::end);

return (int)objFile.tellg() / sizeof(Student);
}

static void LoadStudentObjects(bool ShowStudents)
{
int i;
Student s;
TotalStudents = GetTotalStudents();

for(i=0; i<TotalStudents; i++)
{
ifstream objFile;
objFile.open("Students.dat",ios::in | ios::binary);

objFile.seekg(i*sizeof(Student));
objFile.read(reinterpret_cast<char*>(&s),sizeof(s));

//as the object is now loaded, we can show the data
if(ShowStudents)
s.ShowStudent();

objP[i] = s;

}

}
};


StudentOP::TotalStudents = 0;
/////\\\\\
void main() /////////Menu/////////
{


char ch;

do
{
system("cls");
cout<<"\t\t\t************MeNu************"<<endl;
cout<<"\n\t\t\t\t1.Create Students database"<<endl;
cout<<"\n\t\t\t\t2.Show Studnets Report"<<endl;
cout<<"\n\t\t\t\t3.Exit"<<endl;

cout<<endl<<"\n\t\t\t\tEnter your choice "<<endl;
ch=getch();


switch(ch)
{
case '1':StudentOP::CreateStudentDB();
break;
case '2':
StudentOP::LoadStudentObjects(true);
getch();
break;
case '3':exit(0);

default:
cout<<"\n\t\t\t\tWrong entry";
getch();

}
}while(ch!=3);

//return 0;
}

You have to fully specify the type and scope when defining static members:
int StudentOP::TotalStudents = 0;
Topic archived. No new replies allowed.