Scope error

Using a .h and .cpp file

The .h file is this one and is building fine

#ifndef STATKEEPER_H_
#define STATKEEPER_H_


// Class StatKeeper : creates a class for the output of the airplane simulation.
// This class will keep track of planes taking off, landing, and crashing
// It will also find the average time spent in takeoff queue and landing queue

class StatKeeper {
public:
StatKeeper(int start, int stop);
int totalTime();
int planesTookOff(); //the number of planes that took off in the sim time
void planeTookOff();
int landedPlanes(); //count of landed planes
void landAPlane();
int crahedPlanes(); //count of crashed planes
void crashAPlane();
double avgTakeoffTime(); //average time spent in takeoff queue
double avgLandingTime(); //average time spend in landing queue
void totalTTime(int currentMin, int airplaneFirstInTqueue);
void totalLTime(int currentMin, int airplaneFirstInLqueue);
private:
int countTakeOffs;
int planesLanded;
int crashCount;
int totaLTimeInTakeoffQueue;
int totalTimeInLandingQueue;

};
#endif

The .cpp file keeps giving me a scope error on all my private variables

#include "StatKeeper.h"
#include <cstdlib>



//This constructor takes in parameters start and stop time of airplane simulation
//Initialize the private variables keeping track of planes that tookoff, landed and
// crashed. It also initializes time spent in each queue
StatKeeper::StatKeeper(int start, int stop) {
int startTime=start;
int stopTime=stop;
countTakeOffs = 0;
planesLanded = 0;
crashCount = 0;
totaLTimeInTakeoffQueue = 0;
totalTimeInLandingQueue = 0;
}

int StatKeeper::totalTime() {
int finalTime = startTime - stopTime;
return finalTime;
}

//the number of planes that took off in the sim time
int StatKeeper::planesTookOff() {
return countTakeOffs;
}
void StatKeeper::planeTookOff(){
countTakeOffs = countTakeOffs + 1;
}

//count of landed planes
int StatKeeper::landedPlanes(){
return planesLanded;
}


void StatKeeper::landAPlane(){
planesLanded = planesLanded + 1;
}

//count of crashed planes
int StatKeeper::crahedPlanes(){
return crashCount;
}

void StatKeeper::crashAPlane(){
crashCount = crashCount + 1;
}

//average time spent in takeoff queue
double StatKeeper::avgTakeoffTime(){
return (totaLTimeInTakeoffQueue/countTakeOffs);
}

//average time spend in landing queue
double StatKeeper::avgLandingTime(){
return (totalTimeInLandingQueue/planesLanded);
}

//This method returns nothing. It has takes in the parameters of the current minute
// and the time the airplane first enters the takeoff queue
// It takes these parameters and finds the difference and adds that to the total time
// in takeoff queue
void StatKeeper::totalTTime(int currentMin, int airplaneFirstInTqueue){
totaLTimeInTakeoffQueue = totalTimeInTakeOffQueue + (airplaneFirstInTqueue - currentMin);
}

//This method returns nothing. It has takes in the parameters of the current minute
// and the time the airplane first enters the landing queue
// It takes these parameters and finds the difference and adds that to the total time in
// landing queue
void StatKeeper::totalLTime(int currentMin, int airplaneFirstInLqueue){
totalTimeInLandingQueue = totalTimeInTakeOffQueue + (airplaneFirstInLqueue - currentMin);
}


Here is the error message:

C:\Users\Matt\Documents\Fall 2012 Classes\CS220\Project 1\StatKeeper.cpp:20:18: error: 'startTime' was not declared in this scope
C:\Users\Matt\Documents\Fall 2012 Classes\CS220\Project 1\StatKeeper.cpp:20:30: error: 'stopTime' was not declared in this scope
The error is exactly what it's described as.

In your totalTime function, you are trying to use variables startTime and stopTime, but those variables do not exist.

You do have variables with that name in your StatKeeper constructor, but they are local variables so they go out of scope and die as soon as the constructor exits. They do not exist within the scope of your totalTime function.


If you want to broaden the scope of those vars, you'll have to move them so they're declared in the class along with your other member variables, and not declared locally in your constructor.
Last edited on
I would just like to point out that it is also very helpful if you use the code input for your code so that the readers can decipher it a bit easier. It would also be helpful to use the correct syntax spacing for the various areas of the code.


Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stream>
using namespace std;
int main()
{
     int x;
     x = 10;

            cout << " the integer is " << x << endl;

return 0;

}

Topic archived. No new replies allowed.