C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 31st, 2012, 09:00 PM
eats_food_a_lot eats_food_a_lot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 6 eats_food_a_lot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 11 sec
Reputation 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;
		
}

Reply With Quote
  #2  
Old September 1st, 2012, 01:06 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,381 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 5 m 43 sec
Reputation Power: 4080
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

Reply With Quote
  #3  
Old September 1st, 2012, 01:13 AM
eats_food_a_lot eats_food_a_lot is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 6 eats_food_a_lot User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 13 m 11 sec
Reputation Power: 0
Thanks. I'll have to reconstruct the constructor then.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Why is this C+ attempt to creat a polymorphic object not working?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap