2011-02-05 4 views
0

Je fais un programme de base qui prend une entrée et l'imprime en arrière, mais je reçois étrange erreur de mon variable.Please statique me aider out.ThanksChaîne erreur membre statique

code:

/* 

    Date:5th January 2011 
    Programmer:Fahad 
*/ 
#include <iostream> 
#include <string> 
using namespace std; 
class Persons //A class that will store the name,addresses and id numbers of the users 
{ 
    private: 
     string name_; 
     string address_; 
     int id_number_; 

    public: 
     static int count;//This is the count of the objects created 
     Persons(); 
     void getData(int n); 
     void displayData(int n); 
     //~Persons(); 
}; 
static int count;//initializing the static member 
int main() 
{ 
    cout << "Enter Number Of Persons:"; 
    int n;//This is the number of objects that the user wants to make. 
    cin >> n; 
    Persons *ptr;//A pointer that will be used for the 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 i = 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 '$' To Exit):"; 
     getline(cin,name_,'$'); 
     cout<<endl<<"Enter Address (Press '$' To 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_; 

} 

erreur:

------ Build started: Project: new, Configuration: Debug Win32 ------ 
Compiling... 
program.cpp 
c:\documents and settings\1\my documents\visual studio 2008\projects\new\new\program.cpp(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
Build log was saved at "file://c:\Documents and 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 ========== 
? 

Répondre

2

au lieu de

static int count;//initializing the static member 

écrire

int Persons::count; 
1

Vous devez définir la variable statique int Persons::count; en outre, votre getData &La syntaxe 210 est incorrecte. Vous devez utiliser l'opérateur -> au lieu de l'opérateur ..

2

Votre programme a deux problèmes

Problème n ° 1) de déclaration manquante pour exit(1);.
Solution: Inclure <cstdlib>

Problème no 2) membre statique count déclaré mais non défini.
Solution: Définissez le membre comme int Persons::count; au lieu de static int count.

+0

Le programme fonctionne sans eux y compris l'en-tête.: S –

+0

@fahad: Il ne devrait pas. –

Questions connexes