This code won\'t compile.. it\'s from the book and I\'ve tried several different
ID: 3811570 • Letter: T
Question
This code won't compile.. it's from the book and I've tried several different things, so I must be missing something
Error: 32 13 C:Userswesle_000DocumentsCECSC++Practice CodesBow and Arrow.cpp [Error] 'std::time' has not been declared
//Bow.h
#include <string>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
using std::string;
class Bow
{
//data declarations
string color;
bool drawn;
int arrowsDrawn;
public:
Bow(string aColor); //constructor
~Bow(); //destructor
//methods
void draw();
int fire();
};
//Bow.cpp
Bow::Bow(string aColor)
{
using std::time;
using std::srand;
arrowsDrawn = 10;
drawn = false;
color = aColor;
//seeds the time
srand((unsigned int)time(0));
}
Bow::~Bow()
{
}
//draws bow
void Bow::draw()
{
using std::cout;
drawn = true;
cout << "The " << color << " has been drawn. ";
}
//fires bow if drawn
int Bow::fire()
{
using std::cout;
if(!drawn)
{
cout << color << " has not been drawn "
<< "and therefore could not fire. ";
return 0;
}
int score;
score = rand() % (10 - 0 + 1) + 0;
if (score = 0)
cout << color << " missed the target!!! ";
else
cout << color << " scored " << score << " points!!! ";
return score;
}
Explanation / Answer
The error is coming because in .cpp file you have not included the file "bow.h". You should include following given line in .cpp file:
#include "Bow.h"
Its required because .cpp file get the reference of std::time from bow.h file.