no match for `std::ostream&

problem with reading and writting file.

Page 1 of 1

3 Replies - 976 Views - Last Post: 05 March 2008 - 11:47 AM Rate Topic: -----

#1 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

no match for `std::ostream&

Post icon  Posted 05 March 2008 - 11:33 AM

This program is supposed to read in a data file, organize the data into a vector, and then write the vector into a new file.

My code:
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main()
{
  ifstream fin;   // declare an input file stream
  string file_name, out_stream;
  double x;

  cout << "Enter file name: ";
  cin >> file_name;

  // open file file_name for input
  fin.open(file_name.c_str(), ios::in);  

  // check if file is opened for input
  if (!fin.is_open())
	{
	  cerr << "Unable to open file " << file_name << endl;
	  exit(10);
	}

  // read text from file
  vector<double> v;
  fin >> x;
  while (!fin.eof() && !fin.fail()) {
	  v.push_back(x);
	  fin >>x;
	}

  if (!fin.eof() && fin.fail()) {
	  cerr << "Error reading file " << file_name << endl;
	  exit(20);
	}

  else if (fin.eof() && !fin.fail()) {
	  v.push_back(x);
	}

  // close file stream fin
  fin.close();

  //sort vector
  sort(v.begin(), v.end());

  cout << v <<endl;
  return 0;

  //Step6?

  ofstream fout;

  // open file file_name for output
  // out_stream.c_str() returns a C style string
  fout.open(out_stream.c_str(), ios::out);

  // check if file is opened for output
  if (!fout.is_open())
	{
	  cerr << "Unable to open file " << file_name << endl;
	  exit(10);
	}

  // write vector to file
  fout<< v <<endl;

  // close file stream fout
  fout.close();

  return 0;
}



when i try to compile this I get the error "no match for `std::ostream& << std::vector<double,
std::allocator<double> >&' operator
". I have no idea what this means.

Is This A Good Question/Topic? 0
  • +

Replies To: no match for `std::ostream&

#2 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: no match for `std::ostream&

Posted 05 March 2008 - 11:39 AM

I believe your problem may have something to do with the fact that you are using this code to output a vector to the screen:

 cout << v <<endl;



If you create a for loop to cycle through the vector by using this size as a stopping condition as such:

  for ( int i = 0; i < v.size(); i++ ){
      cout << v[i] <<endl;
      }



That should remedy that situation, as well as the other instances in which you attempt to do that same thing.
Was This Post Helpful? 0
  • +
  • -

#3 nosaj4268  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-February 08

Re: no match for `std::ostream&

Posted 05 March 2008 - 11:44 AM

View Postcaptainhampton, on 5 Mar, 2008 - 11:39 AM, said:

I believe your problem may have something to do with the fact that you are using this code to output a vector to the screen:

 cout << v <<endl;



If you create a for loop to cycle through the vector by using this size as a stopping condition as such:

  for ( int i = 0; i < v.size(); i++ ){
      cout << v[i] <<endl;
      }



That should remedy that situation, as well as the other instances in which you attempt to do that same thing.


Thank you, that fixed it. I knew I should have been using a for loop :D
Was This Post Helpful? 0
  • +
  • -

#4 captainhampton  Icon User is offline

  • Jawsome++;
  • member icon

Reputation: 13
  • View blog
  • Posts: 548
  • Joined: 17-October 07

Re: no match for `std::ostream&

Posted 05 March 2008 - 11:47 AM

No problem, glad I could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1