GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Mar-2010, 07:59
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 51
bostero22 is on a distinguished road

Passing two objects to another class function


Hello I am trying to pass two objects to another class function, however I can't get this to work...

this is when I am calling the function whom I am passing the two objects from main.
CPP / C++ / C Code:
newOfficer.examineParkedCar(newCar, newMeter);

this is the function definition which is located in the class .h
CPP / C++ / C Code:
void examineParkedCar(ParkedCar &a, ParkingMeter &b);

I am getting two errors...

3 C:\cplusplus\CarTicket\policeofficer.cpp In file included from policeofficer.cpp

39 C:\cplusplus\CarTicket\policeofficer.h variable or field `examineParkedCar' declared void

any ideas on how to do this correctly?
  #2  
Old 12-Mar-2010, 09:24
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Passing two objects to another class function


Does your 'examineParkedCar()' return a value?

Perhaps you have a circular dependency issue. That is, one of your header files includes another header that includes the original header that includes the other header that includes...

Without you showing the complete code we can only guess.
  #3  
Old 12-Mar-2010, 09:33
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 51
bostero22 is on a distinguished road

Re: Passing two objects to another class function


It doesn't return anything.. here is the code..


this is main
CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "PoliceOfficer.h"

using namespace std;

int main(int argc, char *argv[])
{
    char choice = '3';
    string policeName = "Default";
        
    cout << "********** POLICE OFFICER SIMULATOR **********" << endl;
    cout << "Hello, Officer! Please Enter your Name: ";
    getline(cin, policeName);
    
    bool loop = true;
    
    
while (loop = true)
{
    
    string policeBadge = "";
    string parkedCarMake = "";
    string parkedCarModel = "";
    string parkedCarColor = "";
    string parkedCarVin = "";
    string parkedCarLicense = "";
    double parkedCarMinutes = 0.0;
    double parkedCarPurchasedMinutes = 0.0;
    
    
    cout << "Hello Officer " << policeName << " !" << endl;
    cout << "Enter your Badge Number: " ;
    getline(cin, policeBadge);
    cout << endl;
    cout << "Enter \"1\" to issue a parking ticket!" << endl;
    cout << "Enter \"2\" to patrol the highway for speeding cars!" << endl;
    cout << "Enter \"3\" to QUIT." << endl;

    cin >> choice;
    cin.ignore();
    
    
    switch(choice)
    {
                  case '1':
                       {
                           cout << "What is the make of the parked car?" << endl;
                           getline(cin, parkedCarMake);
                           cout << "What is the model of the parked car?" << endl;
                           getline(cin, parkedCarModel);
                           cout << "What is the Car's color?" << endl;
                           getline(cin, parkedCarColor);
                           cout << "What is the Car's Vin Number?" << endl;
                           getline(cin, parkedCarVin);
                           cout << "What is the Car's License Plate Number?" << endl;
                           getline(cin, parkedCarLicense); 
                           cout << "How many MINUTES was the parked parked @ this location?" << endl;
                           cin >> parkedCarMinutes;
                           
                           //Make ParkedCar Class
                           ParkedCar newCar;
                           newCar.setParkedCarMake(parkedCarMake); 
                           newCar.setParkedCarModel(parkedCarModel);
                           newCar.setParkedCarColor(parkedCarColor);
                           newCar.setParkedCarVin(parkedCarVin);
                           newCar.setParkedCarLicense(parkedCarLicense);
                           newCar.setParkedCarMinutes(parkedCarMinutes); 
                           
                           
                           cout << "How many minutes did the parked car purchased @ the meter?" << endl;
                           cin >> parkedCarPurchasedMinutes;
                           cin.ignore();
                           
                           //create ParkingMeter Class
                           ParkingMeter newMeter(parkedCarPurchasedMinutes); 
                           
                           //create PolicerOfficer class
                           PoliceOfficer newOfficer;                           
                           newOfficer.setPoliceOfficerName(policeName);
                           newOfficer.setPoliceOfficerBadge(policeBadge);     
                           newOfficer.examineParkedCar(newCar, newMeter);                  
                           
                       }
                       break;
                       
                       
                  case '2':
                       {
                           cout << "CASE 2 ";
                       }
                       break;
                       
                  case '3':
                       loop = false;
                       return 0;
                       break;
                       
    }
    
}
    system("PAUSE");
    return EXIT_SUCCESS;
}


this is policeofficer.h
CPP / C++ / C Code:
// Class automatically generated by Dev-C++ New Class wizard

#ifndef POLICEOFFICER_H
#define POLICEOFFICER_H
#include <string>


using std::string;
using namespace std;

/*
 * # The PoliceOfficer Class: This class should simulate a police officer inspecting parked cars. The class’s responsibilities are:

    * To know the police officer’s name and badge number
    * To Examine ParkedCar object and a ParkingMeter object, and determine whether the time car’s time has expired
    * To issue a parking ticket (generate a ParkingTicket object) if the car’s time has expired. 
 */
class PoliceOfficer
{
    private:
            string policeOfficerName;
            string policeOfficerBadge;  
	public:
		// class constructor
		PoliceOfficer();
		
		void setPoliceOfficerName(string pon)
		{ policeOfficerName = pon; }
		
		void setPoliceOfficerBadge(string pob)
		{ policeOfficerBadge = pob; }
		
		string getPoliceOfficerBadge()
		{ return policeOfficerBadge; }
		
		string getPoliceOfficerName()
		{ return policeOfficerName; }
		
	    void examineParkedCar(ParkedCar &a, ParkingMeter &b);
        
		
		// class destructor
		~PoliceOfficer();
};

#endif // POLICEOFFICER_H


and this is policeofficer.cpp

CPP / C++ / C Code:

// Class automatically generated by Dev-C++ New Class wizard

#include "policeofficer.h" // class's header file

// class constructor
PoliceOfficer::PoliceOfficer()
{
	// insert your code here
}

// class destructor
PoliceOfficer::~PoliceOfficer()
{
	// insert your code here
}


hope this is enough!

thank you! =]
  #4  
Old 12-Mar-2010, 12:09
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Passing two objects to another class function


Quote:
Originally Posted by bostero22
hope this is enough!
It should be. The error is caused because the compiler doesn't know the types ParkedCar and ParkingMeter. To fix this either #include the headers ParkedCar.h and ParkingMeter.h into PoliceOfficer.h or, preferably, use a forward declaration to tell the compiler that ParkedCar and ParkingMeter are class types (a forward declaration is often described as the preferred way mainly because it reduces the compilation time, but if the concept is foreign to you, then just #include the headers):

CPP / C++ / C Code:
// Class automatically generated by Dev-C++ New Class wizard

#ifndef POLICEOFFICER_H
#define POLICEOFFICER_H
#include <string>


using std::string;
using namespace std;

class ParkedCar;
class ParkingMeter;

/*
 * # The PoliceOfficer Class: This class should simulate a police officer inspecting parked cars. The class’s responsibilities are:

    * To know the police officer’s name and badge number
    * To Examine ParkedCar object and a ParkingMeter object, and determine whether the time car’s time has expired
    * To issue a parking ticket (generate a ParkingTicket object) if the car’s time has expired. 
 */
class PoliceOfficer

It should then compile fine.
  #5  
Old 12-Mar-2010, 12:29
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 51
bostero22 is on a distinguished road

Re: Passing two objects to another class function


Actually.. now it gives me no errors.. however I cannot access the member functions of the objects that are being passed... i tried them passing as pointers too.. and the compiler does not recognize them...
  #6  
Old 12-Mar-2010, 13:12
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 51
bostero22 is on a distinguished road

Re: Passing two objects to another class function


here is my whole code again...

I cannot access the member functions of the two objects when I am passing them to PoliceOfficer class.... it does not recognize them.. any ideas?

MAIN
CPP / C++ / C Code:
#include <cstdlib>
#include <iostream>
#include "ParkedCar.h"
#include "ParkingMeter.h"
#include "PoliceOfficer.h"

using namespace std;

int main(int argc, char *argv[])
{
    char choice = '3';
    string policeName = "Default";
        
    cout << "********** POLICE OFFICER SIMULATOR **********" << endl;
    cout << "Hello, Officer! Please Enter your Name: ";
    getline(cin, policeName);
    
    bool loop = true;
    
    
while (loop = true)
{
    
    string policeBadge = "";
    string parkedCarMake = "";
    string parkedCarModel = "";
    string parkedCarColor = "";
    string parkedCarVin = "";
    string parkedCarLicense = "";
    double parkedCarMinutes = 0.0;
    double parkedCarPurchasedMinutes = 0.0;
    
    
    cout << "Hello Officer " << policeName << " !" << endl;
    cout << "Enter your Badge Number: " ;
    getline(cin, policeBadge);
    cout << endl;
    cout << "Enter \"1\" to issue a parking ticket!" << endl;
    cout << "Enter \"2\" to patrol the highway for speeding cars!" << endl;
    cout << "Enter \"3\" to QUIT." << endl;

    cin >> choice;
    cin.ignore();
    
    
    switch(choice)
    {
                  case '1':
                       {
                           cout << "What is the make of the parked car?" << endl;
                           getline(cin, parkedCarMake);
                           cout << "What is the model of the parked car?" << endl;
                           getline(cin, parkedCarModel);
                           cout << "What is the Car's color?" << endl;
                           getline(cin, parkedCarColor);
                           cout << "What is the Car's Vin Number?" << endl;
                           getline(cin, parkedCarVin);
                           cout << "What is the Car's License Plate Number?" << endl;
                           getline(cin, parkedCarLicense); 
                           cout << "How many MINUTES was the parked parked @ this location?" << endl;
                           cin >> parkedCarMinutes;
                           
                           //Make ParkedCar Class
                           ParkedCar newCar;                           
                           
                           newCar.setParkedCarMake(parkedCarMake); 
                           newCar.setParkedCarModel(parkedCarModel);
                           newCar.setParkedCarColor(parkedCarColor);
                           newCar.setParkedCarVin(parkedCarVin);
                           newCar.setParkedCarLicense(parkedCarLicense);
                           newCar.setParkedCarMinutes(parkedCarMinutes); 
                           
                           ParkedCar *newCarPtr;
                           newCarPtr = &newCar;
                           
                           
                           cout << "How many minutes did the parked car purchased @ the meter?" << endl;
                           cin >> parkedCarPurchasedMinutes;
                           cin.ignore();
                           
                           //create ParkingMeter Class
                           ParkingMeter newMeter(parkedCarPurchasedMinutes); 
                           
                           ParkingMeter * newMeterPtr;
                           newMeterPtr = &newMeter;
                                                
                           
                           //create PolicerOfficer class
                           PoliceOfficer newOfficer;                           
                                                      
                           newOfficer.setPoliceOfficerName(policeName);
                           newOfficer.setPoliceOfficerBadge(policeBadge);     
                           newOfficer.examineParkedCar(newCarPtr, newMeterPtr);  //HERE IM PASSING THE TWO OBJECTS AS POINTERS                 
                           
                       }
                       break;
                       
                       
                  case '2':
                       {
                           cout << "CASE 2 ";
                       }
                       break;
                       
                  case '3':
                       loop = false;
                       return 0;
                       break;
                       
    }
    
}
    system("PAUSE");
    return EXIT_SUCCESS;
}



This is ParkedCar class
CPP / C++ / C Code:
// Class automatically generated by Dev-C++ New Class wizard
 /*    * To know the car’s make, model, color, VIN, and license number.
    * To know the number of minutes that the car has been parked.*/
#ifndef PARKEDCAR_H
#define PARKEDCAR_H
#include <string>

using std::string;

class ParkedCar
{
    private:
            string parkedCarMake;
            string parkedCarModel;
            string parkedCarColor;
            string parkedCarVin;
            string parkedCarLicense;
            double parkedCarMinutes;
	public:
		// class constructor
		ParkedCar();
		
        void setParkedCarMake(string ma) 
        { parkedCarMake = ma; }
        
        void setParkedCarModel(string mo) 
        { parkedCarModel = mo; }
        
        void setParkedCarColor(string c) 
        { parkedCarColor = c; }
        
        void setParkedCarVin(string v) 
        { parkedCarVin = v; }
        
        void setParkedCarLicense(string l) 
        { parkedCarLicense = l; }
        
        void setParkedCarMinutes(double m) 
        { parkedCarMinutes = m; }
        
        string getParkedCarMake()
        { return parkedCarMake; }
        
        string getParkedCarModel()
        { return parkedCarModel; }
        
        string getParkedCarColor()
        { return parkedCarColor; }
        
        string getParkedCarVin()
        { return parkedCarVin; }
        
        string getParkedCarLicense()
        { return parkedCarLicense; }
        
        double getParkedCarMinutes()
        { return parkedCarMinutes; }
        
		// class destructor
		~ParkedCar();
};

#endif // PARKEDCAR_H


ParkingMeter class
CPP / C++ / C Code:
 
// Class automatically generated by Dev-C++ New Class wizard

#ifndef PARKINGMETER_H
#define PARKINGMETER_H
#include <string>

using std::string;

/*
 * # The ParkingMeter Class: This class should simulate a parking meter. The class’s only responsibility is:

    * To know the number of minutes of parking time that has been purchased.
 */
class ParkingMeter
{
    private:
            double parkedCarMinutes;
	public:
		// class constructor
		ParkingMeter(double m)
        {parkedCarMinutes = m;}
        
        void setParkedCarMinutes(double m)
        { parkedCarMinutes = m; }
        
        double getParkedCarMinutes()
        { return parkedCarMinutes; }
        
		// class destructor
		~ParkingMeter();
};

#endif // PARKINGMETER_H


PoliceOfficer class

CPP / C++ / C Code:
// Class automatically generated by Dev-C++ New Class wizard

#ifndef POLICEOFFICER_H
#define POLICEOFFICER_H
#include <string>
#include "parkedcar.h"
#include "parkingmeter.h"
#include <cstdlib>
#include <iostream>

using std::string;
using namespace std;

/*class ParkedCar;
class ParkingMeter;*/

/*
 * # The PoliceOfficer Class: This class should simulate a police officer inspecting parked cars. The class’s responsibilities are:

    * To know the police officer’s name and badge number
    * To Examine ParkedCar object and a ParkingMeter object, and determine whether the time car’s time has expired
    * To issue a parking ticket (generate a ParkingTicket object) if the car’s time has expired. 
 */
class PoliceOfficer
{
    private:
            string policeOfficerName;
            string policeOfficerBadge;  
	public:
		// class constructor
		PoliceOfficer();
		
		void setPoliceOfficerName(string pon)
		{ policeOfficerName = pon; }
		
		void setPoliceOfficerBadge(string pob)
		{ policeOfficerBadge = pob; }
		
		string getPoliceOfficerBadge()
		{ return policeOfficerBadge; }
		
		string getPoliceOfficerName()
		{ return policeOfficerName; }
		
	    void examineParkedCar(ParkedCar *a, ParkingMeter *b) //HERE I CANNOT READ THE OBJECTS .. I TRIED a-> etc.. does not RECOGNIZE THEM
	    {  }
	      
		
		// class destructor
		~PoliceOfficer();
};

#endif // POLICEOFFICER_H

  #7  
Old 12-Mar-2010, 13:42
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 388
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Passing two objects to another class function


Quote:
Originally Posted by bostero22
any ideas?
Not really. You don't even use the objects in your examineParkedCar() method, so clearly the code you posted does not generate the error you describe. In fact, I tried calling the getParkedCarMake() method of 'a' inside examineParkedCar() and it compiled and ran.

Three things I can think of:

1. You used the forward declaration, but then tried to write the method examineParkedCar() inline inside your PoliceOfficer class. This won't do. If you want to do this, then you need to include the header files. Otherwise the compiler has no way of knowing that ParkedCar has this-and-this method and it does that-and-that.

2. You tried to access the methods with the indirect member access operator (->). Use '->' only with pointer types, otherwise use '.' .

3. You changed the parameters to pointers instead of references, but tried to access them with the direct member access operator (.) .



But, it's only guessing. Without direct copy-pastes of the erorr messages there's no way of knowing.

Quote:
now it gives me no errors.. however I cannot access the member functions of the objects that are being passed... i tried them passing as pointers too.. and the compiler does not recognize them...
I cannot think of a case where you for some reason cannot access a member function but still get no errors. Please show this in code.



EDIT:

Here's a sample examineParkedCar() method:
CPP / C++ / C Code:
void examineParkedCar(ParkedCar *a, ParkingMeter *b)
        {
            cout << "\n\nHello, it's officer " << policeOfficerName
                 << " here. I'm examining a car here...";
            cout << "\nThe make is... " << a->getParkedCarMake()
                 << " and that's it for now.\n\n";
                
        }

And here's a sample run of your program with this:

Code:
********** POLICE OFFICER SIMULATOR ********** Hello, Officer! Please Enter your Name: John Smith Hello Officer John Smith ! Enter your Badge Number: thk Enter "1" to issue a parking ticket! Enter "2" to patrol the highway for speeding cars! Enter "3" to QUIT. 1 What is the make of the parked car? Audi What is the model of the parked car? A... 7? What is the Car's color? Gray What is the Car's Vin Number? Huh? What is the Car's License Plate Number? KTM-432 How many MINUTES was the parked parked @ this location? 76 How many minutes did the parked car purchased @ the meter? 1 Hello, it's officer John Smith here. I'm examining a car here... The make is... Audi and that's it for now. Hello Officer John Smith ! Enter your Badge Number: ^C
  #8  
Old 12-Mar-2010, 14:33
bostero22 bostero22 is offline
Junior Member
 
Join Date: Oct 2008
Posts: 51
bostero22 is on a distinguished road

Re: Passing two objects to another class function


ahh.. I tried my code in my home pc and it works perfect now.. It didnt back when i was at work!

thanks alot!! =]
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
run script command on ns2.26 newbie06 Computer Software Forum - Linux 66 16-Jan-2010 10:53
Compiling C btrieve programs in VS 2005 emanresu C Programming Language 1 16-Nov-2009 03:19
Problem executing nam-1.13 RodolfoAlvizu Computer Software Forum - Linux 20 28-Feb-2009 15:23
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 15:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:01.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.