c++ - String static member error -
i making basic program takes input , prints getting strange error static variable.please me out.thanks
code:
/* date:5th january 2011 programmer:fahad */ #include <iostream> #include <string> using namespace std; class persons //a class store name,addresses , id numbers of users { private: string name_; string address_; int id_number_; public: static int count;//this count of objects created persons(); void getdata(int n); void displaydata(int n); //~persons(); }; static int count;//initializing static member int main() { cout << "enter number of persons:"; int n;//this number of objects user wants make. cin >> n; persons *ptr;//a pointer used dynamic memory allocation. /*exception handling*/ //////////////////////////////////////////////////////////////////// try { //ptr=new [sizeof(persons) * n]; ptr=new persons[n]; } catch(bad_alloc xa) { cout<<"sorry,program can not continue"; cin.get(); exit(1); } ///////////////////////////////////////////////////////////////////// for(int = 0; i< n; i++) { ptr[i].getdata(n); } for(int j = 0; j< n; j++) { ptr[j].displaydata( n ); } cin.get(); delete[] ptr; return 0; } /*function definitions*/ persons::persons() { name_=""; address_=""; id_number_=0; count++; } void persons::getdata(int n) { cout<<"enter name (press '$' exit):"; getline(cin,name_,'$'); cout<<endl<<"enter address (press '$' exit):"; getline(cin,address_,'$'); cout<<endl<<"enter identitiy card number:"; cin>>id_number_; } void persons::displaydata(int n) { cout<<"name:"<<name_; cout<<endl<<"address:"<<address_; cout<<endl<<"identitiy card number:"<<id_number_; }
error:
------ build started: project: new, configuration: debug win32 ------ compiling... program.cpp c:\documents , settings\1\my documents\visual studio 2008\projects\new\new\program.cpp(23) : error c4430: missing type specifier - int assumed. note: c++ not support default-int build log saved @ "file://c:\documents , settings\1\my documents\visual studio 2008\projects\new\new\debug\buildlog.htm" new - 1 error(s), 0 warning(s) ========== build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== ?
instead of
static int count;//initializing static member
write
int persons::count;
Comments
Post a Comment