ifstream f(strFile); problem???
Apr 20, 2011 at 2:08am UTC
77 C:\Users\dominic\Desktop\prj5\yahoo ex 1.cpp variable `std::ifstream f' has initializer but incomplete type
^^^^thats the error I get and I dont know how to fix it.
heres my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <iterator>
#include <istream>
using namespace std;
class CDog{
private :
string strOwner;
int weight;
int iStay;
int iCost;
int iCategory;
public :
static int get_perday(int iWeight, int & iCategory)
{
if (iWeight >= 10 && iWeight < 30) {
iCategory = 1;
return 12;
}
else if (iWeight >= 30 && iWeight < 50) {
iCategory = 2;
return 18;
}
else if (iWeight >= 50) {
iCategory = 3;
return 22;
}
iCategory = 0;
return 0;
}
static const int iColNameWidth;
static const int iColWeightWidth;
static const int iColStayWidth;
static const int iColCostWidth;
static const int iColCostPrecision;
//const int& get_stay() const {return iStay;}
//const int& get_weight() const {return weight;}
const int & get_cost() const {return iCost;}
//const string& get_owner() const {return strOwner;}
const int get_category() const {return iCategory;}
CDog():strOwner("" ), weight(0), iStay(0){}
friend ostream& operator <<(ostream& o, const CDog& dog)
{
o << "\n" << setw(CDog::iColNameWidth) << dog.strOwner <<
setw(CDog::iColWeightWidth) << dog.weight <<
setw(CDog::iColStayWidth) << dog.iStay <<
setw(CDog::iColCostWidth) << fixed << setprecision(2) << (float )dog.iCost;
return o;
}
// In case the weight of the dog is smaller than 10 there won't be no charges
// or the dog will be denied. Here no charges is assumed!!!!!
friend istream& operator >>(istream& i, CDog& dog)
{
i >> dog.strOwner >> dog.weight >> dog.iStay;
dog.iCost = CDog::get_perday(dog.weight, dog.iCategory) * dog.iStay;
return i;
}
};
const int CDog::iColNameWidth = 20;
const int CDog::iColWeightWidth = 7;
const int CDog::iColStayWidth = 5;
const int CDog::iColCostWidth = 9;
const int CDog::iColCostPrecision = 2;
class CDogs{
private :
vector<CDog> v;
string strFilename;
int iCount;
public :
CDogs(const string& strFile)
{
ifstream f(strFile);
strFilename = strFile;
copy(istream_iterator<CDog>(f), istream_iterator<CDog>(), back_inserter(v));
int iTotal = 0;
int iCategory[4] = {0};
for (unsigned i = 0; i < v.size(); i++) {
iTotal += v[i].get_cost();
iCategory[v[i].get_category()] += 1;
}
cout << "\n" <<
setw(CDog::iColNameWidth) << "Dog Owner" <<
setw(CDog::iColWeightWidth) << "Weight" <<
setw(CDog::iColStayWidth) << "Stay" <<
setw(CDog::iColCostWidth) << "Cost" ;
copy(v.begin(), v.end(), ostream_iterator<CDog>(cout, "" ));
//cout<<"\n"<<"---------------------------… <<
cout << "\n" << v.size() << " dogs total $" << fixed << setprecision(2) << (float )iTotal << " income." ;
cout <<"\nThere are"
<< "\n" << iCategory[1] << " dogs in Category " << 1
<< "\n" << iCategory[2] << " dogs in Category " << 2
<< "\n" << iCategory[3] << " dogs in Category " << 3
<< "\n" << iCategory[0] << " dogs outside all Categories " ;
}
};
int main()
{
CDogs("a.txt" );
cin.get();
}
Apr 20, 2011 at 4:05am UTC
You left out #include <fstream>
and another thing, ifstream f(strFile);
should give an error, because the std::ifstream
constructor does not accept std::string
object -change it to CString/const char *
first
Topic archived. No new replies allowed.