1

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

sokkyoku
  • 2,161
  • 1
  • 20
  • 22
Jake quin
  • 738
  • 1
  • 9
  • 25
  • 6
    Don't spam tags! That's not C! – too honest for this site Sep 08 '16 at 15:29
  • Try using a different variable name than `list`. If you have `using namespace std;`, the compiler is going to be using `std::list` and it gets confused with your variable name. – Thomas Matthews Sep 08 '16 at 15:35
  • Please edit your post with the definition of `list`. In one statement it's a one-dimensional variable, in another, it is a 2 dimensional variable. – Thomas Matthews Sep 08 '16 at 15:36
  • 1
    This isn't the problem, but just construct `outfile` with the name of the file: `std::ofstream outfile("sales.lst");`. And there's no need to check whether it is open; stream inserters can handle a stream that's not in a valid state. And, finally, don't bother to `close` the file: the destructor for `outfile` will do that. – Pete Becker Sep 08 '16 at 15:37
  • 1
    OT, it should be i **<** list.size(). Same for j probably. – Bob__ Sep 08 '16 at 15:37
  • As @Bob__ mentioned, you have an off-by-one error there – sokkyoku Sep 08 '16 at 15:40
  • In the table you are printing, the first header is called "ID". There's any chance that you meant `outfile< – Bob__ Sep 08 '16 at 15:47

3 Answers3

3
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 <=.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
2

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0

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.

George
  • 2,101
  • 1
  • 13
  • 27
Jake quin
  • 738
  • 1
  • 9
  • 25