Vector iteration and I/O problems
Dec 5, 2008 at 11:12am UTC
Can someone please give me a tip as to were I am going wrong with my code as I am having problems with iterating through my vector of products. Also can you tell me if I am on the correct lines regards the output of the data to file.
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
#include <iomanip.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <ctype.h>
#include <vector.h>
#include "derived.h"
main()
{
ifstream in;
ofstream out;
class product product_rec;
std::vector<product> p;
p.push_back( product("Sony PSP" ,1001,99.99) );
p.push_back( product("Sony PS3" ,1002,299.99) );
p.push_back( product("Xbox" ,1003,79.99) );
p.push_back( product("Xbox 360" ,1004,179.99) );
in.open("L:\\product.dat" ,ios::binary);
while (!in.eof())
{
in.read((char *) & product_rec,sizeof (class product);
if (in.good())
{
cout << "\n" << setw(24) << product_rec.product_name
<< setw(10) << product_rec.product_code
<< setw(10) << product_rec.unit_price;
clrscr();
cout << "File Successfully Written to DAT File" ;
out.open("L:\\product.dat" ,ios::binary|ios::app);
out << setw(24) << product_rec.product_name;
out << setw(10) << product_rec.product_code;
out << setw(10) << product_rec.unit_price;
out.close();
}
}
std::vector<product>::iterator it;
for ( it = p.begin(); it != p.end(); it++ )
cout << *it << endl;
}
I am getting 2 errors:
1 - Is on the
in.read((char *) & product_rec,sizeof (class product);
where I am told function call missing
2 - Is on the
1 2
for ( it = p.begin(); it != p.end(); it++ )
cout << *it << endl;
where I am told this is an illegal structure operation.
Last edited on Dec 5, 2008 at 11:14am UTC
Dec 5, 2008 at 1:35pm UTC
Your first line isn't even syntactically correct since there are more open parens than close parens.
Your second problem is that product needs to have operator<< defined for it.
Dec 5, 2008 at 1:46pm UTC
Hey again jsmith. Thank you for pointing out the first error I had not noticed it and after adding another bracket the error message has dissapeard.
How do I define the operator ?
Dec 5, 2008 at 2:36pm UTC
something like this:
1 2 3 4 5
ostream& operator << ( ostream& os, product prod)
{
//Do you output
return os;
}
It is best to be declared as product friend
Dec 5, 2008 at 5:52pm UTC
Either
1 2 3 4
template < typename charT, typename Traits> friend
std::basic_ostream<charT, Traits>& operator <<(
std::basic_ostream<charT, Traits>& os, const product& p )
{ return os << ...; }
or as Bazzy said, except pass prod by const reference instead of by value.
Dec 5, 2008 at 7:37pm UTC
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
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <ctype.h>
#include <vector.h>
#include "derived.h"
main()
{
ifstream in;
ofstream out;
class product product_rec;
std::vector<product> p;
p.push_back( product("Sony PSP" ,1001,99.99) );
p.push_back( product("Sony PS3" ,1002,299.99) );
p.push_back( product("Xbox" ,1003,79.99) );
p.push_back( product("Xbox 360" ,1004,179.99) );
in.open("L:\\product.dat" ,ios::binary);
while (!in.eof())
{
in.read((char *) & product_rec,sizeof (class product));
if (in.good())
{
cout << "\n" << setw(24) << product_rec.product_name
<< setw(10) << product_rec.product_code
<< setw(10) << product_rec.unit_price;
clrscr();
cout << "File Successfully Written to DAT File" ;
out.open("L:\\product.dat" ,ios::binary|ios::app);
out << setw(24) << product_rec.product_name;
out << setw(10) << product_rec.product_code;
out << setw(10) << product_rec.unit_price;
out.close();
}
}
ostream& operator << (ostream& os, const product& p)
{
return os << p.product_name()<<'\t'
<< p.product_code()<<'\t'
<< p.unit_price() << endl;
}
}
I have changed the code to your recommendation but am still encountering several errors.
1. Improper use of typedef 'ostream'
2. Improper use of typedef 'ostream'
3. Undefined symbol os
4. Expression syntax
5. Statement missing
6. Compund statement missing
Last edited on Dec 5, 2008 at 7:50pm UTC
Dec 5, 2008 at 7:47pm UTC
My version is the more general version of an ostream operator, though in most cases Bazzy's specialization will work.
std::ostream is essentially a basic_ostream instantiated on a char.
To use Bazzy's method,
1 2 3 4 5 6 7
// Add this inside class product's declaration:
friend ostream& operator <<( ostream& os, const product& p )
{
// Output whatever you want to output
// eg, os << p.product_name;
return os;
}
Topic archived. No new replies allowed.