Home » Language IDEs » C / C++ IDE (CDT) » Problems with class instantiation and a destructor reference
Problems with class instantiation and a destructor reference [message #168679] |
Mon, 24 April 2006 22:16  |
Eclipse User |
|
|
|
Hi All,
I created a Managed Make C++ Project and am having problems with class
instantiation and a destructor reference.
I saved my class, constructors, destructor, and function prototypes in file
lab4gammenth.h. I then defined the constructors, destructor, and functions
in lab4gammenthFunc.cpp. I instantiated the class in lab4gammenthMain.cpp
and get the error "Undefined Reference to 'employee::~employee()'.
If you take a look at the files, they also include another class, invItem().
That's code that came with my book. It also has constructors, a destructor
and a couple of functions. It instantiates fine (currently commented out for
testing).
I don't understand why Eclipse is treating those two classes differently.
Thanks - Doris
|
|
|
Re: Problems with class instantiation and a destructor reference [message #168693 is a reply to message #168679] |
Tue, 25 April 2006 02:32  |
Eclipse User |
|
|
|
Hi Doris,
the class employee declares a destructor (~employee), which is never
defined, like e.g.
employee::~employee()
{
delete [] lastName;
delete [] firstName;
delete [] title;
}
HTH,
Toni
Doris Gammenthaler wrote:
> Hi All,
>
> I created a Managed Make C++ Project and am having problems with class
> instantiation and a destructor reference.
>
> I saved my class, constructors, destructor, and function prototypes in file
> lab4gammenth.h. I then defined the constructors, destructor, and functions
> in lab4gammenthFunc.cpp. I instantiated the class in lab4gammenthMain.cpp
> and get the error "Undefined Reference to 'employee::~employee()'.
>
> If you take a look at the files, they also include another class, invItem().
> That's code that came with my book. It also has constructors, a destructor
> and a couple of functions. It instantiates fine (currently commented out for
> testing).
>
> I don't understand why Eclipse is treating those two classes differently.
>
> Thanks - Doris
>
>
>
>
>
>
> /*---------------------------------------------------------- ------------------------------------
> PROGRAMMER: Doris Gammenthaler
> LANGUAGE: C
> CLASS: CSE 1320-005, "Intermediate Programming in C", Spring 2006
> PLATFORM: OMEGA
> OS: UNIX
> COMPILER: g++
> ASSIGNMENT: Lab 4
> ASSIGNED: 4/12/06
> DUE: 5/4/06, 12:30 PM
> FILE NAME: lab4gammenthMain.cpp
> FILES USED:
> CONCEPTS: C++ Object Oriented Development
> WEIGHT: 10%
> PURPOSE: Driver file for this application
> ------------------------------------------------------------ ------------------------------------------*/
>
> #include <iostream.h>
> #include "lab4gammenth.h"
>
> int main()
> {
>
> invItem default_item; // Note 1
> cout << "Initial values for default_item: ";
> default_item.display();
> default_item.changeId( "AB1111F" );
> default_item.changeNum( 50 );
> cout << "Modified values for default_item: ";
> default_item.display();
> invItem copied_item( default_item ); // Note 2
> cout << "Initial values for copied_item: ";
> copied_item.display();
>
> invItem another_item( "A1234H" ); // Note 3
> cout << "Initial values for another_item: ";
> another_item.display();
> invItem another_copy = another_item; // Note 4
> cout << "Initial values for another_copy: ";
> another_copy.display();
>
> employee e;
>
> return 0;
> }
>
> /*---------------------------------------------------------- ------------------------------------
> PROGRAMMER: Doris Gammenthaler
> LANGUAGE: C
> CLASS: CSE 1320-005, "Intermediate Programming in C", Spring 2006
> PLATFORM: OMEGA
> OS: UNIX
> COMPILER: g++
> ASSIGNMENT: Lab 4
> ASSIGNED: 4/12/06
> DUE: 5/4/06, 12:30 PM
> FILE NAME: lab4gammenth.h
> FILES USED:
> CONCEPTS: C++ Object Oriented Development
> WEIGHT: 10%
> PURPOSE: Class definitions
> ------------------------------------------------------------ ------------------------------------------*/
>
> class employee {
>
> public:
> employee(); // default constructor
> employee( const employee& empl ); // constructs a copy
> employee( char * ln, char mi, char * fn, int id, char * t, double sal );
>
> ~employee(); // destructor
>
> // ----- mutators -----
> bool setLastName( char *lastName );
> bool setMi ( char mi );
> bool setFirstName( char *firstName );
> bool setEmplId ( long int emplId );
> bool setTitle( char *title );
> bool setSalary ( double salary );
>
> // ----- accessors -----
> const char * getLastName() const { return lastName; }
> const char getMi() const { return mi; }
> const char * getFirstName() const { return firstName; }
> const long int getEmplId() const { return emplId; }
> const char * getTitle() const { return title; }
> const double getSalary() const { return salary; }
> void print(); // display all data values of the class object
>
> private:
> char *lastName;
> char mi;
> char *firstName;
> long int emplId;
> char *title;
> double salary;
> };
>
>
> // invItem.h
> //
> // Declaration of the invItem class
> class invItem {
> public:
> invItem(); // Note 2
> // PRECONDITION: none
> //
> // POSTCONDITION: The data members of the object have
> // been initialized to default values.
>
> invItem( const invItem& item ); // Note 3
> // PRECONDITION: none
> //
> // POSTCONDITION: the constructed object is a copy of item.
>
> invItem( char *the_id, int the_num = 0 ); // Note 4
> // PRECONDITION: none
> //
> // POSTCONDITION: the object is constructed with id equal the_id
> // andnum_available equal the_num.
>
> ~invItem(); // Note 5
> // PRECONDITION: none
> //
> // POSTCONDITION: the object has been properly destroyed.
>
> bool changeNum( int change_amt ); // Note 6
> // PRECONDITION: change_amt should be the amount by which the
> // number in the inventory has changed.
> // POSTCONDITION: If change_amt is positive, num_available
> // will be incremented by change_amt and
> // true will be returned. If change_amt is
> // negative, the return value will be true
> // if num_available+change_amt
> // is greater or equal to 0. The new value
> // of num_available will be num_available +
> // change_amt. If change_amt is negative and
> // num_available+change_amt is also negative,
> // the return value will be false and
> // change_amt will remain unchanged.
>
> bool changeId( char *newId ); // Note 7
> // PRECONDITION: newId should be a null terminated
> // C style string.
> // POSTCONDITION: The id has been changed to newId; the return
> // value is true.
>
> const char * getId() const { return id; } // Note 8
> // PRECONDITION: none
> //
> // POSTCONDITION: the return value is the item's id.
>
> int whatsLeft() const { return num_available; }
> // PRECONDITION: none
> //
> // POSTCONDITION: the return value is num_available
>
> void display();
> // PRECONDITION: none
> //
> // POSTCONDITION: the id and number in inventory
> // have been displayed on standard output
>
> private:
> char *id; // Note 1
> int num_available;
> };
>
> /*---------------------------------------------------------- ------------------------------------
> PROGRAMMER: Doris Gammenthaler
> LANGUAGE: C
> CLASS: CSE 1320-005, "Intermediate Programming in C", Spring 2006
> PLATFORM: OMEGA
> OS: UNIX
> COMPILER: gcc
> ASSIGNMENT: Lab 4
> ASSIGNED: 4/12/06
> DUE: 5/4/06, 12:30 PM
> FILE NAME: lab4gammenthConst.cpp
> FILES USED:
> CONCEPTS: Structures and unions, command line arguments, file i/o, linked lists
> WEIGHT: 13%
> PURPOSE: Contains constants and function prototypes
> ------------------------------------------------------------ ------------------------------------------*/
>
> /* Include Files */
> #include <ctype.h>
> #include <stdio.h>
> #include <string.h>
>
> /* Constant Definitions */
> #define ARR_SIZE 19
> #define TEXT_BUFF 80
> #define FILE_NAME 20
> #define IN_BUFF 512
> #define MAX_CAT 6 // 5 + '/0'
> #define MAX_NAME 81 // 80 + '/0'
> #define MAX_ITEMS 10
>
>
>
> /*---------------------------------------------------------- ------------------------------------
> PROGRAMMER: Doris Gammenthaler
> LANGUAGE: C
> CLASS: CSE 1320-005, "Intermediate Programming in C", Spring 2006
> PLATFORM: OMEGA
> OS: UNIX
> COMPILER: g++
> ASSIGNMENT: Lab 4
> ASSIGNED: 4/12/06
> DUE: 5/4/06, 12:30 PM
> FILE NAME: lab4gammenthFunc.cpp
> FILES USED:
> CONCEPTS: C++ Object Oriented Development
> WEIGHT: 10%
> PURPOSE: Class memeber functions definitions
> ------------------------------------------------------------ ------------------------------------------*/
>
> #include <iostream.h>
> #include <string.h>
> #include "lab4gammenth.h"
>
> employee::employee()
> {
> lastName = new char[1];
> lastName[0] = '0';
> mi = '0';
> firstName = new char[1];
> firstName[0] = '0';
> emplId = 0;
> title = new char[1];
> title[0] = '0';
> salary = 0.00;
> }
>
>
> /*
> employee::employee ( const employee& empl ) // constructs a copy
> {
> lastName = NULL;
> mi = ' ';
> firstName = NULL;
> emplId = 0;
> title = NULL;
> salary = 0.00;
> *this = empl;
> }
>
> employee::employee ( char * ln, char m, char * fn, int id, char * t, double sal )
> {
> lastName = new char [ strlen ( ln ) + 1 ];
> strcpy ( lastName, ln );
> mi = m;
> firstName = new char [ strlen ( fn ) + 1 ];
> strcpy ( firstName, fn );
>
> if ( id < 0 )
> {
> id = 0;
> cout << "Id cannot be negative, setting Id to 0" << endl;
> }
> else
> emplId = id;
>
> if ( strcmp(t, "clerk") ||
> strcmp(t, "owner") ||
> strcmp(t, "cashier") ||
> strcmp(t, "custodian") ||
> strcmp(t, "secretary") )
> {
> title = new char [ strlen ( t ) + 1 ];
> title = t;
> }
> else
> {
> title = new char[1];
> title[0] = '0';
> cout << "Title is invalid, setting Title to blank" << endl;
> }
>
> if ( sal < 0 )
> {
> cout << "Salary cannot be negative, setting Salary to 0" << endl;
> salary = 0;
> }
> else
> salary = sal;
> }
>
> */
>
> // ----- mutators -----
>
> bool employee::setLastName( char *ln )
> {
> lastName = ln;
> return true;
> }
>
> bool employee::setMi( char m )
> {
> mi = m;
> return true;
> }
>
> bool employee::setFirstName( char *fn )
> {
> firstName = fn;
> return true;
> }
>
> bool employee::setEmplId ( long int id )
> {
> if ( id < 0 )
> {
> id = 0;
> cout << "Id cannot be negative, setting Id to 0" << endl;
> return false;
> }
> else
> {
> emplId = id;
> return true;
> }
> }
>
> bool employee::setTitle( char *t )
> {
> if ( strcmp(t, "clerk") ||
> strcmp(t, "owner") ||
> strcmp(t, "cashier") ||
> strcmp(t, "custodian") ||
> strcmp(t, "secretary") )
> {
> title = new char [ strlen ( t ) + 1 ];
> title = t;
> return true;
> }
> else
> {
> title = new char[1];
> title[0] = '0';
> cout << "Title is invalid, setting Title to blank" << endl;
> return false;
> }
> }
>
> bool employee::setSalary ( double sal )
> {
> if ( sal < 0 )
> {
> cout << "Salary cannot be negative, setting Salary to 0" << endl;
> salary = 0;
> return false;
> }
> else
> {
> salary = sal;
> return true;
> }
> }
>
> // ----- accessors -----
>
> void employee::print() // display all data values of the class object
> {
> cout << "employees" << endl;
> cout << "-------------------" << endl;
> cout << lastName <<" ";
> cout << mi <<" ";
> cout << firstName <<" ";
> cout << emplId <<" ";
> cout << title <<" ";
> cout << salary <<" ";
> }
>
>
>
> // inv_item.cpp
>
> // Implementation of inv_item class
>
> invItem::invItem() // Note 1
> {
> id = new char[5];
> strcpy( id, "####" );
> num_available = 0;
> }
>
> invItem::invItem( const invItem& item ) // Note 2
> {
> id = new char[ strlen( item.id ) + 1 ];
> strcpy( id, item.id );
> num_available = item.num_available;
> }
>
> invItem::invItem( char *the_id, int the_num ) // Note 3
> {
> id = new char[ strlen( the_id ) + 1 ];
> strcpy( id, the_id );
> num_available = the_num;
> }
>
> invItem::~invItem() // Note 4
> {
> delete[] id;
> }
>
> bool invItem::changeNum( int change_amt )
> {
> if ( num_available + change_amt >=0 ) {
> num_available += change_amt;
> return true;
> }
> return false;
> }
>
> bool invItem::changeId( char *newId ) // Note 5
> {
> char *tmp = new char[ strlen( newId ) + 1 ];
> strcpy( tmp, newId );
> delete[] id;
> id = tmp;
> return true;
> }
>
> void invItem::display()
> {
> cout << "Id: " << id
> << " - Available " << num_available
> << endl;
> }
|
|
|
Goto Forum:
Current Time: Thu Jul 17 15:38:50 EDT 2025
Powered by FUDForum. Page generated in 0.04746 seconds
|