#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0

    Why is this C+ attempt to creat a polymorphic object not working?


    I am trying to make a vehicle pointer which points to certain vehicle status values from the Vehicle* vector. Basicly I have a Vehicle base class with vehicle types as the derived classes and the pointer is then suppose to be a polymorphic object with a status of what the person has based on the bool value set from the prompt. I am starting out with car and it is not working. I want Vehicle to point to car_status() which will then return stat from Car in the vector. I get this error on run.cpp- 1>run.cpp(49): error C2061: syntax error : identifier 'car_status' and I get this error on Vehicle.cpp Vehicle.cpp(47): error C2061: syntax error : identifier 'car_status'. If someone could help me out and tell me what I am doing wrong it would be great.


    Code:
    run.cpp
    #include "Vehicle.h"
    #include "Car.h"
    #include "Truck.h"
    #include "Van.h"
    #include "Motorcycle.h"
    #include <iostream>
    #include <string>
    
    #include <vector>
    using namespace std;
    
    int main()
    {
    	
    	/* Implicit parameter */
    	Vehicle prompt;
    	Car car_value;
    	/* Set the value of vehicles */
    	car_value.set_car();
    	/* Loop */
    	bool go = true;
    	while (go){
    
    	/* The prompt */
    	prompt.vehicle_select();
    	
    	/* Constructors of the derived classes */
    	Car(prompt.get_type());
    	Truck(prompt.get_type());
    	Van(prompt.get_type());
    	Motorcycle(prompt.get_type());
    	
    	/* Continue?*/
    	string remainder;
    	getline(cin, remainder);
    	cout << "Continue (y/n): " << endl;
    	string con;
    	getline(cin, con);
    	if (con != "y")
    		go = false;
    		
    	}
    	/* Program Ending */
    	string the_car;
    	bool hasc = false;
    	/* Get car boolean */
    	hasc = car_value.has_car();
            /* Vehicle pointer */
    	vector<Vehicle*> owned_vtypes(1);
            /* Trying to place the car status in element 0 */
    	owned_vtypes[0] = new car_status(the_car); // Error is here
           /* trying to place the car status by using a member function */
    	prompt.vtypes(owned_vtypes, hasc, the_car);
           /* None of it worked if it ever does I would improve this part to display the vector */
    	for(int i = 0; i < 1; i++){
    		cout << owned_vtypes[i] << endl;}
    
    	cout << "Have a nice day." << endl;
    	system("Pause");
    	return 0;
    }
    Vehicle.h
    #ifndef VEHICLE_H
    #define VEHICLE_H
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class Vehicle
    {
    public:
    	Vehicle();
    	Vehicle(string vehicle_type);
    	void vehicle_select();
    	virtual void vtypes(vector<Vehicle*>& v, bool ifv, string the_car);
    	virtual string get_type() const;
    
    
    private:
    	string type;
    	
    };
    Vehicle.cpp
    #endif
    #include "Vehicle.h"
    #include <iostream>
    #include <string>
    
    #include <vector>
    using namespace std;
    Vehicle::Vehicle()
    {
    
    }
    
    Vehicle::Vehicle(string vehicle_type)
    {
    	type = vehicle_type;
    	
    }
    
    void Vehicle::vehicle_select()
    {
    	unsigned short num = 0; 
    	cout << "Please select the number of which vehicle you have: " << endl;
    	cout << "1. Car: " << endl;
    	cout << "2. Truck: " << endl;
    	cout << "3. Van: " << endl;
    	cout << "4. Motorcycle: " << endl;
    	cin >> num;
    	if (num == 1){
    		type = "Car";}
    	if (num == 2){
    		type = "Truck";}
    	if (num == 3){
    		type = "Van";}
    	if (num == 4){
    		type = "Motorcyle";}
    
    }
    
    string Vehicle::get_type() const
    {
    	return type;
    }
    
    void Vehicle::vtypes(vector<Vehicle*>& v, bool ifv, string the_car)
    {
    	
    	if(ifv == true)
    		v[0] = new car_status(the_car); // Error is here
    }
    
    Car.h
    #ifndef CAR_H
    #define CAR_H
    #include <iostream>
    #include "Vehicle.h"
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class Car : public Vehicle
    {
    public:
    	Car();
    	/* Construct to return bool type 
    	and cout message from base class */
    	Car(string type);
    
    	/* Uses the virtual keyword to get
    	the type from the public base interface
    	and uses the private encapsulated string */
    	virtual string get_type() const;
    	/* Sets car value
    	to false */
    	void set_car();
    	/* Test to see if the person
    	has the vehicle type */
    	bool has_car() const;
    	/* Returns string for the 
    	vector status for vehicle*/
    	virtual string car_status();
    	
    private:
    	bool is_car;
    	string stat;
    };
    #endif
    Car.cpp
    #include "Car.h"
    #include "Vehicle.h"
    #include <iostream>
    
    #include <string>
    #include <vector>
    using namespace std;
    
    Car::Car()
    {
    
    }
    
    Car::Car(string type)
    	: Vehicle(type)
    {
    	if(type == "Car"){
    		is_car = true;
    		
    	}
    
    	
    	
    }
    
    string Car::get_type() const
    {
    	
    	return Vehicle::get_type();
    }
    
    void Car::set_car()
    {
    	
    	is_car = false;
    
    }
    
    bool Car::has_car() const
    {
    		return is_car;
    	
    }
    
    string Car::car_status()
    	
    {
    	if(is_car == false)
    	stat = "Car: No";
    	
    	if(is_car == true)
    		stat = "Car: Yes";
    	return stat;
    		
    }
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    Are you sure you understand how to construct an object in C++? car_status() is not a constructor, so you can't arbitrarily call new on it. Also, if you have a Vehicle* object, you can't invoke car_status() on it because the Vehicle class doesn't have that method.

    I think you need to take a step back and understand how to construct a basic C++ object before you even try to write any polymorphic stuff.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    6
    Rep Power
    0
    Thanks. I'll have to reconstruct the constructor then.

IMN logo majestic logo threadwatch logo seochat tools logo