Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
C++ classes help...program written but i get linker errors?
Im working on a program that calculates speed when accelerating and braking but it uses classes... i need help here is my code so far
#include<iostream>
#include<cstring>
using namespace std;
class Car
{
private:
int yearModel;
string make;
int speed;
public:
void Car::setspeed(int);
void Car::setmake(string);
void Car::setyearModel(int);
int Car::getspeed() const;
string Car::getmake() const;
int Car::getyearModel() const;
int accelerate(int);
int brake(int);
};
int Car::getspeed() const
{
return speed;
}
string Car::getmake() const
{
return make;
}
int Car::getyearModel() const
{
return yearModel;
}
void Car::setspeed(int a)
{
speed = a;
}
void Car::setmake(string b)
{
make = b;
}
void Car::setyearModel(int c)
{
yearModel = c;
}
int accelerate(int num)
{
num += 5;
return speed;
}
int brake(int num)
{
num += 5;
return speed;
}
int main()
{
Car vehicle;
int x = 0, y = 0;
string name;
cout << "What is the initial speed? ";
cin >> x;
cout << "What is the make? ";
cin >> name;
cout << "What is the year the car was made? ";
cin >> y;
vehicle.setspeed( x );
vehicle.setmake( name );
vehicle.setyearModel( y );
cout << " The make of your car is " << vehicle.getmake() << endl;
cout << "Your car was built in " << vehicle.getyearModel() << endl;
cout << "The intial speed is " << vehicle.getspeed() << endl;
cout << "After the car has accelerated, the speed is " << vehicle.accelerate(x) << endl;
cout << "After the car has applied the brakes, the speed is " << vehicle.brake(x) << endl;
cout << "After the car has accelerated, the speed is " << vehicle.accelerate(x) << endl;
cout << "After the car has accelerated, the speed is " << vehicle.accelerate(x) << endl;
cout << "After the car has applied the brakes, the speed is " << vehicle.brake(x) << endl;
cout << "After the car has applied the brakes, the speed is " << vehicle.brake(x) << endl;
cout << "After the car has accelerated, the speed is " << vehicle.accelerate(x) << endl;
cout << "After the car has applied the brakes, the speed is " << vehicle.brake(x) << endl;
cout << "After the car has accelerated, the speed is " << vehicle.accelerate(x) << endl;
cout << "After the car has applied the brakes, the speed is " << vehicle.brake(x) << endl;
system ("PAUSE");
return 0;
}
[Linker error] undefined reference to `Car::brake(int)' (5 times)
[Linker error] undefined reference to `Car::brake(int)' (5 times)
2 Answers
- 1 decade agoFavorite Answer
I have no idea what your linker errors might be since you did not deign to tell us, but your accelerate() and brake() functions are clearly broken.
If it is your intention to change the speed in these functions, you must do exactly that. CHANGE THE SPEED.
Instead, you do nothing to the speed but you increment or decrement the parameter passed in to the function. This has no effect since it is passed by value. Thus, "num" changes just before going out of scope, leaving "speed" exactly the same as it was before.
Try "speed += num" and "speed -= num" in these functions, since "speed" is the instance variable that the Car class uses to keep track of the current speed of the car.
- ?Lv 44 years ago
Your important.cpp is calling a function CheckValidLat() that takes a waft as a controversy. The document containing CheckValidLat() desires to be compiled and related with the main significant.cpp. If CheckValidLat() function is already defined in important.cpp then verify the information form of the argument being handed to it.