Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,484 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,753 people online right now. Registration is fast and FREE... Join Now!




no match for `std::ostream&

 
Closed TopicStart new topic

no match for `std::ostream&, problem with reading and writting file.

nosaj4268
5 Mar, 2008 - 10:33 AM
Post #1

New D.I.C Head
*

Joined: 26 Feb, 2008
Posts: 11


My Contributions
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:
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.
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: No Match For `std::ostream&
5 Mar, 2008 - 10:39 AM
Post #2

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
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:

cpp

cout << v <<endl;


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

cpp

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.

User is offlineProfile CardPM
+Quote Post

nosaj4268
RE: No Match For `std::ostream&
5 Mar, 2008 - 10:44 AM
Post #3

New D.I.C Head
*

Joined: 26 Feb, 2008
Posts: 11


My Contributions
QUOTE(captainhampton @ 5 Mar, 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:

cpp

cout << v <<endl;


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

cpp

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 biggrin.gif
User is offlineProfile CardPM
+Quote Post

captainhampton
RE: No Match For `std::ostream&
5 Mar, 2008 - 10:47 AM
Post #4

Jawsome++;
Group Icon

Joined: 17 Oct, 2007
Posts: 518



Thanked: 2 times
Dream Kudos: 825
My Contributions
No problem, glad I could help
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 12/2/08 07:09PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month