// Fig. 7.9: Fig7_9_employ1.h
// An employee class
#ifndef FIG7_9_EMPLOY1_H
#define FIG7_9_EMPLOY1_Hclass Employee{
public:
Employee( const char*, const char* );  // constructor
~Employee(); // destructor
const char *getFirstName() const; // return first name
const char *getLastName() const; // return last name // static member function
static int getCount(); // return # objects instantiatedprivate:
char *firstName;
char *lastName; // static data member
static int count;   // number of objects instantiated
};#endif// Fig. 7.9: Fig7_9_employ1.cpp
// member function definitions for class Employee
#include <iostream.h>
#include <string.h>
#include <assert.h>
#include "Fig7_9_employ1.h"// Initialize the static data member
int Employee::getCount() { return count; }// Constructor dynamically allocates space for the
// first and last name and uses strcpy to copy
// the first and last names into the object
Employee::Employee( const char *first, const char *last )
{
firstName = new char[ strlen( first ) +1 ];
assert( firstName != 0 ); // ensure memory allocated
strcpy( firstName, first ); lastName = new char[ strlen( last ) + 1 ];
assert( lastName != 0 ); // ensure memory allocated
strcpy( lastName, last );
++count; // increment static count of employees
cout << "Employee constructor for " << firstName
<< ' ' << lastName << " called." << endl;
}// Destructor deallocates dynamically allocated memory
Employee::~Employee()
{
cout << "~Employee() called for " << firstName
<< ' ' << lastName << endl;
delete[] firstName; // recapture memory
delete[] lastName; // recapture memory
--count; // decrement static count of employees
}// Return first name of employee
const char *Employee::getFirstName() const
{
// Const before return type prevents client modifying
// priate data. Client should copy returned string before
// destructor deletes storage to prevent undefined pointer.
return firstName;
}// Return last name of employee
const char *Employee::getLastName() const
{
// Const before return type prevents client modifying
// private data. Client should copy returned string before
// destructor deletes storage to prevent undefined pointer
return lastName;
}// Fig. 7.9: Fig7_9.cpp
// Driver to test the employee class
#include <iostream.h>
#include "Fig7_9_employ1.h"int main()
{
cout << "Number of employees before instantiation is "
<< Employee::getCount() << endl; // use class name Employee *e1Ptr = new Employee( "Susan", "Baker" );
Employee *e2Ptr = new Employee( "Robert", "Jones" ); cout << "Number of employees after instantiation is "
<< e1Ptr->getCount; cout << "\n\nEmployee 1: "
<< e1Ptr->getFirstName()
<< " " << e1Ptr->getLastName()
<< "\nEmployee 2 "
<< e2Ptr->getFirstName()
<< " " << e2Ptr->getLastName() << "\n\n"; delete e1Ptr; //recapture memory
e1Ptr = 0;
delete e2Ptr; //recapture memory
e2Ptr = 0; cout << "Number of employees after deletion is "
<< Employee::getCount() << endl;
return 0;
}--------------------------------------------------我用vc.net调试总是有这个出错提示。我不明白,请大家帮忙看看
Fig fatal error LNK1120: 1 个无法解析的外部命令
Fig error LNK2020: 无法解析的标记 (0A000010) ?count@Employee@@0HA

解决方案 »

  1.   

    Employee类的静态变量:count没有初始化,在*.cpp中加上:int Employee::count = 0;
      

  2.   

    Employee类的静态变量:count没有初始化,在*.cpp中加上:int Employee::count = 0;
      

  3.   

    回复人: In355Hz(好象一条狗) (  ) 信誉:100  2002-09-21 10:59:00  得分:0  
     
     
      Employee类的静态变量:count没有初始化,在*.cpp中加上:int Employee::count = 0;  
     
    Top 
     
     回复人: Fishcat() (  ) 信誉:100  2002-09-21 11:12:00  得分:0  
     
     
      Employee类的静态变量:count没有初始化,在*.cpp中加上:int Employee::count = 0;
      
     
    Top 
     
     回复人: In355Hz(好象一条狗) (  ) 信誉:100  2002-09-21 11:15:00  得分:0  
     
     
      ????