Click here to Skip to main content
15,308,835 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is this the proper solution program to the question, if not then what changes are to be made please tell.

This program is showing 2 errors why ?
error:field 'myVehicle' has incomplete type vehicle [9]
note: definition of 'class vehicle' is not complete until closing brace
In member function 'void vehicle::valid_mileage (int)'
error: a function-definition is not allowed here before '{' token

What I have tried:

#ifndef VEHICLE_H
#define VEHICLE_H

#include <iostream>
#include <fstream>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;

//Vehicle Class
class Vehicle {
protected:
	Vehicle myVehicle[9];
	string make;  //make
    string model; // model
    string color;  // color
    int	year;  // year
    int mileage;  // miles on car
	string type;  //Type of vehicle

public:
	//Constructor that will set information for a new car
	void New_vehicle (string a, string b, string c, int d, int e) 
	{make = a; model = b; color = c; year = d; mileage = e;}
	
	Vehicle(); //Default constructor
	Vehicle(string, string, string, int, int, string);
	//mutator and accessor functions
	void setMake(string);
    void setModel(string);
    void setColor(string);
    void setYear(int);
    void setMileage(int);
	void setType(string);

    string getMake();
    string getModel();
    string getColor();
    int getYear();
    int getMileage();
	string getType();

	//Check mileage to see if valid
	void valid_mileage(int);

	//virtual function
	virtual void details() {
	}

};
//Sets to default values
Vehicle::Vehicle() {
	make = " ";
    model = " ";
    color = " ";
    year = 0;
    mileage = 0;
	type = " ";
}

Vehicle::Vehicle(string make, string model, string color, int year, int mileage, string type) {
    Vehicle::make =  make;
    Vehicle::model = model;
    Vehicle::color = color;
    Vehicle::year = year;
    valid_mileage(mileage);
	Vehicle::type = type;
}

void Vehicle::setMake(string make) {
    Vehicle::make = make;
}

void Vehicle::setModel(string model) {
    Vehicle::model = model;
}

void Vehicle::setColor(string color) {
    Vehicle::color = color;
}

void Vehicle::setYear(int year) {
    Vehicle::year = year;
}

void Vehicle::setMileage(int mileage) {
    valid_mileage(mileage);
}

void Vehicle::setType(string type) {
	Vehicle::type = type;
}


string Vehicle::getMake() {
    return make;
}
string Vehicle::getModel() {
    return model;
}
string Vehicle::getColor() {
    return color;
}
int Vehicle::getYear() {
    return year;
}
int Vehicle::getMileage() {
    return mileage;
}

string Vehicle::getType() {
	return type;
}


void Vehicle::valid_mileage(int mileage) {
    if (mileage>=0)
        Vehicle::mileage=mileage;
    else {
        Vehicle::mileage=0;
        cout << "WARNING! You have entered invalid mileage!\n";
    }

	    Vehicle& getVehicle(int n) {
        return myVehicle[n];
    }
};

#endif 
Posted
Updated 10-Mar-22 16:38pm
v2
Comments
Sammy Magara 24-Nov-21 7:43am
   
Define a class called vehicle that will store two items of information about a vehicle: The fuel capacity and the fuel consumption in kilometers per litre and prototype of four functions a default constructor and a parameterized constructor that initializes the Taxi fuel capacity to 20 litres and Taxi consumption to 10 kpl , a destructor and a friend function that uses the values from the constructors to calculates the taxi range (the maximum distance it would travel if its fuel tank is full without refueling) NB. Show only the prototype of the function in the class

You have spurious and missing closing curly brackets.
There is an extra one after the definition of details, a missing one at the end of valid_mileage, and so on.

This is one of the reasons I hate the 1TB system - it makes it a lot harder to match up brackets when they are in the wrong place as auto indent doesn't work...
   
Quote:
Is this the proper solution program to the question
Nope, that is not the solution: You are providing just the Vehicle class, you should provide as well both the Car and the Bike class.

Moreover, what is the purpose of the array of Vehicles into the Vehicle class? What is the purpose of the type data member?

Try to keep the number of details at minimum and provide the required class hierarchy.
   
The first error is sourced here:
class Vehicle {
protected:
    Vehicle myVehicle[9];
// ...
};
You can't use a class instance as a member of it's own class.
error:field 'myVehicle' has incomplete type vehicle [9]
note: definition of 'class vehicle' is not complete until closing brace
The class is not defined at that point so that the compiler does not know how to handle it.

The second error is sourced by the code block
C++
Vehicle& getVehicle(int n) {
    return myVehicle[n];
}
in your valid_mileage() function. It looks like it has been copied and pasted to the wrong location.

Note also that :: is the scope resolution operator for name spaces and to access static members of a class. To access non static members of a class when the member name is also used as parameter, use the this pointer - cppreference.com[^]:
C++
void Vehicle::setMake(string make) {
    this->make = make;
}
   
Comments
munsine 28-Mar-18 6:03am
   
Please correct and post the code.
Jochen Arndt 28-Mar-18 6:18am
   
The first error can't be corrected. It is a design problem and requires a different implementation. Or just remove that member. It has no purpose and is also not required for the assignment.

The second: Just remove that block.

The member access: Replace "Vehicle::" with "this->"

There are probably more errors. I answered just the question regarding the error messages and a problem I found.

See also solution 2 about what to do (deriving classes).

I will not write the code for you. Nor will other's here. We will help with specific problems like your error messages. But we will not do homework for other's or write code on demand.
munsine 28-Mar-18 7:56am
   
ok, please tell from which line I need to remove the codes and what needed to replace.
Jochen Arndt 28-Mar-18 8:22am
   
Did you read my previous comment?

What is the purpose of the myVehicle[9] member?
If there is none, remove it.

The second: Just remove that block.
What is the purpose of that block at that position?
It looks like it has been inserted at the wrong position.

The above refers to code written by you. I have only explained what is wrong resulting in the thrown error message. But only you will know why and for what purpose you have done that. So if I tell you something like "replace this with that on line #x" it would not really help you.

The member access: Replace "Vehicle::" with "this->"
at all occurences.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900