Ok, I have a class project to basically design a program that handles classes and vehicles. What I have so far is far from the ending product. I have made 3 classes, car, vehicle, and motorcycle. Car and motorcycle are dervied from vehicle. The car class has a set const variable for number of wheels to 4. mpg and model name are the other variables. Motorcycle is a class with const for 2 wheels and model and mpg. Vehicle only has number of wheels and mpg.

Here is my code thus far.

Class: car.h

#ifndef CAR_H
#define CAR_H

#include <string>

#include "vehicle.h"

using std::string;

class Car  : public Vehicle {
public:
  Car();
  Car(string const& model, double milesPerGallon);

  void setModel(string const& model);

  string const& getModel() const;

  virtual void print() const;

private:
  string model;
};

#endif

Class motorcycle.h:

#ifndef MOTORCYCLE_H
#define MOTORCYCLE_H

#include <string>

#include "vehicle.h"

using std::string;

class MotorCycle : public Vehicle {
public:
  MotorCycle();
  MotorCycle(string const& model, double milesPerGallon);

  void setModel(string const& model);

  string const& getModel() const;

  virtual void print() const;

private:
  string model;
};

#endif

class vehicle.h

#ifndef VEHICLE_H
#define VEHICLE_H

class Vehicle {
public:
  Vehicle();
  Vehicle(int wheels, double milesPerGallon);

  void setWheels(int wheels);
  void setMilesPerGallon(double milesPerGallon);

  double getMilesPerGallon() const;
  int    getWheels() const;

  virtual void print() const;

private:
  int wheels;
  double milesPerGallon;
};

#endif

source files:
car.cpp:

#include "car.h"

#include <iostream>

using namespace std;

Car::Car() {
}

Car::Car(string const& model, double milesPerGallon)
    : Vehicle(4, milesPerGallon), model(model) {
}

void Car::setModel(string const& model) {
  this->model = model;
}

string const& Car::getModel() const {
  return model;
}

void Car::print() const {
  Vehicle::print();
  cout << "Car model: " << model << "\n";
}

motorcycle.cpp

#include "motorcycle.h"

#include <iostream>

using namespace std;

MotorCycle::MotorCycle() {
}

MotorCycle::MotorCycle(string const& model, double milesPerGallon)
    : Vehicle(2, milesPerGallon), model(model) {
}

void MotorCycle::setModel(string const& model) {
  this->model = model;
}

string const& MotorCycle::getModel() const {
  return model;
}

void MotorCycle::print() const {
  Vehicle::print();
  cout << "Motorcycle model: " << model << "\n";
}

vehicle.cpp

#include "vehicle.h"

#include <iostream>

using namespace std;

Vehicle::Vehicle() {
  wheels = 0; milesPerGallon = 0;
}

Vehicle::Vehicle(int wheels, double milesPerGallon)
  : wheels(wheels), milesPerGallon(milesPerGallon) {
}

void Vehicle::setWheels(int wheels) {
  this->wheels = wheels;
}

void Vehicle::setMilesPerGallon(double milesPerGallon) {
  this->milesPerGallon = milesPerGallon;
}

double Vehicle::getMilesPerGallon() const {
  return milesPerGallon;
}

int Vehicle::getWheels() const {
  return wheels;
}

void Vehicle::print() const {
  cout << "Wheels number: " << wheels << "\n"
    << "Miles per gallon: " << milesPerGallon << "\n";
}

and main.cpp

#include <iostream>

#include "car.h"
#include "vehicle.h"
#include "motorcycle.h"

using namespace std;

int main() {
  double mpg; 
  int wheelss;
  Car c("Ford", 8);
  MotorCycle mc("Bike", 5);
  

  
  cout << "Please enter the miles per gallon for vehicle:" << endl;
  cin >> mpg;
  Vehicle milesPerGallon(double mpg);
  cout << "Please enter the number of wheels for the vehicle:" << endl;
   cin >> wheelss;
  Vehicle wheels(int wheelss);

 
  Vehicle.print();
  c.print();
  mc.print();

  return 0;
}

Problem I am having is getting users to input the code instead of having it statically held by the program. So i am attempting this with the vehicle class. I am prompting for mpg and number of wheels. when I compile I get the follow error:
Error 1 error C2143: syntax error : missing ';' before '.' \Desktop\vehicle\main.cpp 25 test15
Error 2 error C2143: syntax error : missing ';' before '.' \Desktop\vehicle\main.cpp 25 test15

I tried googleing the error and found that it typically has something to do with omission in previos lines. I have checked previous lines and have semi colons through out. Any help/guidance would be great.

THank you in advance for your time and effort!

Dani AI

Generated

The compiler error stems from statements that the compiler treats as function declarations rather than object creation. As noted, a line that looks like Type name(param-list); is parsed as a prototype (the classic "most vexing parse"), so no object is constructed and a later attempt to call a method on a non-existent instance yields the "missing ';' before '.'" message. See the cppreference explanation for the most vexing parse: .

Two straightforward fixes:

  • Construct a Vehicle instance with the user values, or
  • Default-construct an instance and then call setters.

Example patterns (use whichever fits the class API):

Vehicle v;                 // default ctor
v.setMilesPerGallon(mpg);
v.setWheels(wheelss);
v.print();                 // call on instance, not on the type

or

Vehicle v2(wheelss, mpg);  // direct construction (wheels first)
v2.print();

Additional notes and best practices:

  • Print is non-static; always call it on an object (e.g., v.print()), not on the class (Vehicle.print()).
  • Keep parameter order straight: the Vehicle ctor expects (int wheels, double mpg).
  • Give clear variable names (avoid typos like wheelss) to reduce confusion.
  • If polymorphism and containers of mixed vehicles are planned, add a virtual destructor to the base class to avoid undefined behavior:
virtual ~Vehicle() {}

’s suggestion to move I/O and construction into small helper functions (so main just orchestrates calls) is a good next step for cleaner design and easier testing.

Recommended Answers

All 2 Replies

Vehicle milesPerGallon(double mpg);
  cout << "Please enter the number of wheels for the vehicle:" << endl;
   cin >> wheelss;
  Vehicle wheels(int wheelss);

You are declaring function prototypes here. where-as i think, you need to call the functions there. Guess you should probably check that out!

Ok, I have a class project to basically design a program that handles classes and vehicles. What I have so far is far from the ending product. I have made 3 classes, car, vehicle, and motorcycle. Car and motorcycle are dervied from vehicle. The car class has a set const variable for number of wheels to 4. mpg and model name are the other variables. Motorcycle is a class with const for 2 wheels and model and mpg. Vehicle only has number of wheels and mpg.

Here is my code thus far.

Class: car.h

#ifndef CAR_H
#define CAR_H

#include <string>

#include "vehicle.h"

using std::string;

class Car  : public Vehicle {
public:
  Car();
  Car(string const& model, double milesPerGallon);

  void setModel(string const& model);

  string const& getModel() const;

  virtual void print() const;

private:
  string model;
};

#endif

Class motorcycle.h:

#ifndef MOTORCYCLE_H
#define MOTORCYCLE_H

#include <string>

#include "vehicle.h"

using std::string;

class MotorCycle : public Vehicle {
public:
  MotorCycle();
  MotorCycle(string const& model, double milesPerGallon);

  void setModel(string const& model);

  string const& getModel() const;

  virtual void print() const;

private:
  string model;
};

#endif

class vehicle.h

#ifndef VEHICLE_H
#define VEHICLE_H

class Vehicle {
public:
  Vehicle();
  Vehicle(int wheels, double milesPerGallon);

  void setWheels(int wheels);
  void setMilesPerGallon(double milesPerGallon);

  double getMilesPerGallon() const;
  int    getWheels() const;

  virtual void print() const;

private:
  int wheels;
  double milesPerGallon;
};

#endif

source files:
car.cpp:

#include "car.h"

#include <iostream>

using namespace std;

Car::Car() {
}

Car::Car(string const& model, double milesPerGallon)
    : Vehicle(4, milesPerGallon), model(model) {
}

void Car::setModel(string const& model) {
  this->model = model;
}

string const& Car::getModel() const {
  return model;
}

void Car::print() const {
  Vehicle::print();
  cout << "Car model: " << model << "\n";
}

motorcycle.cpp

#include "motorcycle.h"

#include <iostream>

using namespace std;

MotorCycle::MotorCycle() {
}

MotorCycle::MotorCycle(string const& model, double milesPerGallon)
    : Vehicle(2, milesPerGallon), model(model) {
}

void MotorCycle::setModel(string const& model) {
  this->model = model;
}

string const& MotorCycle::getModel() const {
  return model;
}

void MotorCycle::print() const {
  Vehicle::print();
  cout << "Motorcycle model: " << model << "\n";
}

vehicle.cpp

#include "vehicle.h"

#include <iostream>

using namespace std;

Vehicle::Vehicle() {
  wheels = 0; milesPerGallon = 0;
}

Vehicle::Vehicle(int wheels, double milesPerGallon)
  : wheels(wheels), milesPerGallon(milesPerGallon) {
}

void Vehicle::setWheels(int wheels) {
  this->wheels = wheels;
}

void Vehicle::setMilesPerGallon(double milesPerGallon) {
  this->milesPerGallon = milesPerGallon;
}

double Vehicle::getMilesPerGallon() const {
  return milesPerGallon;
}

int Vehicle::getWheels() const {
  return wheels;
}

void Vehicle::print() const {
  cout << "Wheels number: " << wheels << "\n"
    << "Miles per gallon: " << milesPerGallon << "\n";
}

and main.cpp

#include <iostream>

#include "car.h"
#include "vehicle.h"
#include "motorcycle.h"

using namespace std;

int main() {
  double mpg; 
  int wheelss;
  Car c("Ford", 8);
  MotorCycle mc("Bike", 5);
  

  
  cout << "Please enter the miles per gallon for vehicle:" << endl;
  cin >> mpg;
  Vehicle milesPerGallon(double mpg);
  cout << "Please enter the number of wheels for the vehicle:" << endl;
   cin >> wheelss;
  Vehicle wheels(int wheelss);

 
  Vehicle.print();
  c.print();
  mc.print();

  return 0;
}

Problem I am having is getting users to input the code instead of having it statically held by the program. So i am attempting this with the vehicle class. I am prompting for mpg and number of wheels. when I compile I get the follow error:
Error 1 error C2143: syntax error : missing ';' before '.' \Desktop\vehicle\main.cpp 25 test15
Error 2 error C2143: syntax error : missing ';' before '.' \Desktop\vehicle\main.cpp 25 test15

I tried googleing the error and found that it typically has something to do with omission in previos lines. I have checked previous lines and have semi colons through out. Any help/guidance would be great.

THank you in advance for your time and effort!

you have some minor errors send me a pm and i can help you because we can better implement your program where the only thing you have in main are a few function calls, nothin else

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.