Reputation: 742
I don't understand why this code gives me error
void printSalesFile(vector< vector<float> > & list)
{
ofstream outfile;
outfile.open("sales.lst", ios::out);
if (outfile.is_open())
{
outfile << setw(6) << right << "ID"
<< setw(12) << "January"
<< setw(12) << "Febuary"
<< setw(12) << "March"
<< setw(12) << "April"
<< setw(12) << "May"
<< setw(12) << "June"
<< setw(12) << "July"
<< setw(12) << "August"
<< setw(12) << "September"
<< setw(12) << "October"
<< setw(12) << "November"
<< setw(12) << "December" << endl;
for (unsigned int i = 0; i <= list.size(); i++)
{
outfile << setw(6) << right << list[i]; //i don't understand why it says there's an error here.
for(int j = 0; j <= 11; j++)
outfile << setw(12) << right << list[i][j];
outfile << endl;
}
}
outfile.close();
}
I have tried deleting it and pasting the things I wrote above that works but still get the errors.
Here is the error message:
D:\QT\Salesperson\main.cpp:295: error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'
outfile << setw(6) << list[i];
^
As for the text file, it has 2 lines, 1 for the header and another that has values all set to 0
Upvotes: 1
Views: 184
Reputation: 742
Thank you very much for pointing out the mistakes in my code, forgive me since im only starting out.
for (unsigned int i=0; i<list.size();i++)
{
outfile<<setw(6)<<right<<list[i][0];
for(int j = 1; j<13; j++)
outfile<<setw(12)<<right<<list[i][j];
outfile<<endl;
}
i have taken the mistakes that everybody pointed out and changed part of the code into this one.It now works thank you very much!
what threw me off was the error pointing to << which made me not look how on list was incorrectly written.
Upvotes: 0
Reputation: 76235
outfile<<setw(6)<<right<<list[i]; //i don't understand why it says there's an error here.
It's because there's no stream inserter for std::vector<float>
.
Note also that for(unsigned int i = 0; i <= list.size(); ++i)
will run off the end of the list. Use <
instead of <=
.
Upvotes: 3
Reputation: 33854
Since you are using namespace std
don't declare list
as an object it is likley that you are shadowing this class name. Or better yet, dont use using namespace std
because of this problem.
But your problems wont stop there, look at this line:
outfile<<setw(6)<<right<<list[i];
list
is a vector< vector<float> >
so list[i]
will resolve to a vector<float>
, how do you print that?
In this line:
for (unsigned int i=0; i<=list.size();i++)
should be i < list.size()
, what happens when i == list.size()
, you are going to reference vector[vector.size()]
which will invoke undefined behaviour (remember that array referencing starts at 0).
There may be other things as well.
Upvotes: 2