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

7
  • 6
    Don't spam tags! That's not C! Commented Sep 8, 2016 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. Commented Sep 8, 2016 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. Commented Sep 8, 2016 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. Commented Sep 8, 2016 at 15:37
  • 1
    OT, it should be i < list.size(). Same for j probably. Commented Sep 8, 2016 at 15:37

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

@Ben Check out the last line of the error dump in your link.
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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.