-1

So the problem is that I am trying to use the variable speed in a for loop. The error I'm given is: Uninitialized local variable 'speed' used. The strange thing is that I already declared speed as an int before.

I'm including the header file, and the 2 other files associated with this project. Since there are multiple files involved, I'm using a link to pastebin for all of them, but the UseCar.cpp code will be here as well.

Car.h http://pastebin.com/xn8dnzrH

Car.cpp http://pastebin.com/QYrXDMfe

UseCar.cpp http://pastebin.com/GX8j2vPU

#include "stdafx.h"
#include "Car.h"
#include "Car.cpp"
#include <stdio.h>
#include <string>
#include <iostream>

using namespace std;

int main(){

    Car dealer;// Creates a new Car object
    int yearModel;// Local variables for model year
    int speed;// Local variables for speed
    string make;// Names are kept the same to avoid confusion

    // Calls the accelerate function 5 times and displays the results after each iteration
    for (int i = 0; i < 5; ++i){
        dealer.accelerate(speed);
        cout << "The current speed is: " << dealer.getSpeed() << endl;

    }

    cout << "We are now braking the car" << endl;

    // Calls the brake function 5 times and displays the results after each iteration
    for (int i = 0; i < 5; ++i){
        dealer.brake(speed);
        cout << "The current speed is: " << dealer.getSpeed() << endl;
    }
}
koodeta
  • 83
  • 2
  • 3
  • 14

2 Answers2

1

You defined speed, but you didn't give it an initial value. "Auto" variables, basically ones that are on the stack, are not initialized to anything. Ones of "static storage duration" are zero-initialized. Static storage duration includes ones at "namespace scope" (which includes global variables), static local variables, and static data members.

kec
  • 2,099
  • 11
  • 17
  • I was under the impression that declared variables in C++ were initialized to 0 by default. Is that not the case? – koodeta Apr 30 '14 at 01:00
  • @koodeta Local variables are not automatically initialized to 0. Only global variables are. – ooga Apr 30 '14 at 01:03
  • Only for ones that are of "static storage duration". I'll try to edit the answer. – kec Apr 30 '14 at 01:03
  • @ooga I did initialize the value to be 0 but now a whole host of problems have shown up. Every single function in Car.cpp throws an error in the compiler saying `Car::function name(parameter data type)` is already defined in Car.obj. How would I fix this? – koodeta Apr 30 '14 at 01:09
  • @koodeta Remove the line `#include "Car.cpp"`. And more importantly, maybe take some time to read a beginners book and understand what exactly you're doing. – Praetorian Apr 30 '14 at 01:11
1

There are a number of issues here. First, you are defining a class you want to be public, that is, available in other source files, but you are defining it in a cpp source file rather than a header. If you have to #include a cpp file, you are doing something wrong. It's fine to define classes in cpp files if only the contents of that file needs to know what they are (this is common in PIMPL design), but that is not the case here. Put your class definition in your Car.h and generally speaking never #include a cpp file.

Now, that doesn't answer your initial problem regarding speed. A simple rule to always follow is to always initialize all variables whether they are local or global or member vars. This will avoid a great many bugs you can encounter in c++.

Bill Abrams
  • 320
  • 2
  • 9
  • Thanks for the info! I've adjusted the code to better reflect how I changed it. For the car constructor, is that entire class declaration moved to Car.h and removed from Car.cpp? And what do I use in the #include section for UseCar.cpp? Is it just including Car.h? – koodeta Apr 30 '14 at 01:39
  • First, you will probably save yourself a tremendous amount of time if you look closely at this: http://stackoverflow.com/questions/388242. Second, you must at least move the definition of your class to your .h but you can still implement its functions in the .cpp if you wish (as this is generally wiser) but you can also just keep the entire class in the header file for now. You should also have a header file for UseCar with its class in it, and you must include the header file for Car.h in that UseCar.h. Actually, I just looked at your UseCar.h and your program should only have 1 main() – Bill Abrams Apr 30 '14 at 01:45
  • Thanks. For the implementation of the functions in the .cpp, do you mean that I can keep the original private and public declarations? I cannot make a header file for UseCar as that is not part of the assignment. And don't I only have 1 main()? – koodeta Apr 30 '14 at 02:00
  • I think I did it somewhat correctly. But Now I'm getting about 20 errors in relation to Car.cpp. They are: Use of undefined type 'Car', Undeclared identifier, modifiers not allowed on nonmember functions. How would I fix those? – koodeta Apr 30 '14 at 02:29
  • @koodeta at this point, the answers to your questions would get quite lengthy; to answer them, I would have to write much of what is already written in good c++ books. I very strongly encourage you to put down what you are working on and spend a few hours with a good c++ book: it will clarify everything you have asked. – Bill Abrams Apr 30 '14 at 02:47