|
From: Gregory H. <gr...@fr...> - 2002-10-21 23:49:11
|
I'm having a problem with includes. When I include in main.cpp a header file
that I created, the debugger gets passed main.cpp without any of the header
files included and this causes errors while debugging, such as undelcared
functions, so that I cannot compile the program. It also makes it difficult
to find other errors in my program. I have tried just copying and pasting
the header file and all other linked files into my code and it compiled
successfully, but I'm programming for a class and the teacher will not let
me copy and paste the header file into main.cpp for my final code. I'm using
version 4.9.6.0. Does anybody know of any way to include these files so that
I don't have to copy and paste, or does anybody know which version this
should work in? I've included my four codes below:
/* MAIN.CPP */
// Make a frog move so many steps.
// Created By: Gregory Hart
// Includes
#include "iostream.h"
#include "frog.h"
// Main function
int main()
{
// Declare variables
int position = 0, steps = 0, blanks;
frog myFrog;
// Get number of steps
cout << "How many steps would you like the frog to travel? ";
cin >> steps;
// Print out frog
while (position < steps) {
blanks = 40 + myFrog.move();
for (int a=0; a < blanks; ++a)
cout << " ";
cout << "*" << endl;
++position;
}
cout << "The final position is " << (blanks + 1);
// End program
return 0;
}
/* FROG.H */
// Frog class
// Created By: Gregory Hart
#ifndef _FROG_H
#define _FROG_H
// Includes
#include "dice.h"
class frog
{
public:
frog();
int move();
int getSteps() const;
private:
int mySteps;
int myPosition;
dice myDice;
};
#include "frog.cpp"
#endif
/* DICE.H */
// Metrowerks and Visual version
#ifndef DICE_H
#define DICE_H
#include <stdlib.h> // srand() and rand() are inside of stdlib.h
#include <time.h>
#include <limits.h>
class dice{
public:
dice(int sides); // constructor
int roll(); // return the random roll
int numSides(); // returns how many sides on die
long numRolls(); // returns # times this die has been rolled
private:
int mySides;
long myRollCount;
};
dice::dice(int sides)
// postcondition: all private fields initialized
// randomize() called to set random number generator
{
long time = clock();
srand(time % INT_MAX);
myRollCount = 0;
mySides = sides;
}
int dice::roll()
// postcondition: # of rolls updated, returns random die roll
{
myRollCount++;
return (rand() % mySides) + 1;
}
int dice::numSides()
// postcondition: return # of sides of die
{
return mySides;
}
long dice::numRolls()
// postcondition: return # times die has been rolled
{
return myRollCount;
}
#endif
/* FROG.CPP */
frog::frog()
: mySteps(0), myPosition(0), myDice(2)
{
// used precondition
}
int frog::move()
{
++mySteps;
int n = myDice.roll();
if (myDice.roll() == 1)
--myPosition;
else
++myPosition;
return myPosition;
}
int frog::getSteps() const
{
return mySteps;
}
|