ostream

Why is ranges::ostream_iterator default-constructible?

安稳与你 提交于 2019-12-05 06:55:17
This question follows a discussion in the comments here . In Eric Niebler's ranges-v3 library (which is sort-of becoming part of the standard for C++20), ranges::ostream_iterator is default-constructible - without an ostream. How come? I thought that "dummy" construction with effective construction later is an anti-pattern in C++, a wart we are gradually getting rid of. std::ostream iterator can only be constructed with a stream (for now - before C++20). And it's not as though we can do anything with the default-constructed range::ostream_iterator ... So, what's the deal? This follows the

Visual C++ 2012 cout crashes during run time

荒凉一梦 提交于 2019-12-05 06:12:20
I decided to try Visual Studio 2012 Express today. First thing to do was to write "Hello world!" application, however, I couldn't make it work. I created a Windows console application project, wrote standard code and it resulted in a run-time error. Here's my code: #include <iostream> using namespace std; int main() { cout << "Hello world!" << endl; system("pause"); return 0; } Looks like something is broken (maybe I missed something?). It gets a runtime error at line 7: http://img443.imageshack.us/img443/7497/coutbroken.png Any help please? :) Your CPU is in urgent need of being junked. Your

Is there a good idiom to deal with alternative output streams?

旧街凉风 提交于 2019-12-04 15:39:59
I want to write a simple program that depending on the options passed it the executable will print the output to the screen or to a file. The program is simple. #include<iostream> int main(int argc, char* argv[]){ ... process options... std::ostream& out = ... // maybe std::cout, maybe a *new* std::ofstream; out << "content\n"; } Is there a good idiom to make out refer alternatively to std::cout or a file stream at runtime? I tried with pointers, but it is horrible. I couldn't avoid using pointers (Not to mention that more ugly code is needed to delete the pointer later). #include<iostream>

Segmentation fault (core dumped) Error

强颜欢笑 提交于 2019-12-04 15:24:46
My program compiles fines but upon inputting a file I get a "Segmentation fault (core dumped)" error. Am I not handling the ostream correctly? #include <std_lib_facilities.h> struct Reading { int hour; double temperature; Reading(int h, double t): hour(h), temperature(t) { } bool operator<(const Reading &r) const; }; bool Reading::operator<(const Reading &r) const { // stub version if (temperature < r.temperature){ return true; } else if (r.temperature < temperature) { return false; } } /* * function declarations */ ostream& operator<<(ostream& ost, const Reading &r); vector<Reading> get_temps

Are std::showbase and std::showpos mutually exclusive?

一个人想着一个人 提交于 2019-12-04 08:17:36
This question arose from a discussion I was having about the correct way to output a numeric value using the usual ostream & operator << (ostream &, some_type) for a numeric type in C++. The way I'm familiar with the behavior of std::showbase and std::showpos in each base, they are basically mutually exclusive. That is: in decimal no base is shown, and the '+' is added on positive numbers; whereas in hexadecimal or octal, the base is shown, but a '+' is not shown (nor is a minus), as the value of the type is printed as if it's cast to an unsigned type. For example, this simple (verbose)

Overloaded ostream operator segmentation fault if no endl

风流意气都作罢 提交于 2019-12-04 06:06:38
class foo { public: friend ostream& operator << (ostream &os, const foo &f); foo(int n) : a(n) {} private: vector <int> a; }; ostream& operator << (ostream &os, const foo &f) { for (int i = 0; i < f.a.size(); ++i) os << f.a[i] << " "; os << endl; // why is this line a must? } int main(void) { foo f(2); cout << f << endl; return 0; } In the above code, if the marked line is removed, there will be a segment fault error, can someone explain why? ostream& operator << (ostream &os, const foo &f) { for (int i = 0; i < f.a.size(); ++i) os << f.a[i] << " "; os << endl; // why is this line a must? } is

inheriting ostream and streambuf problem with xsputn and overflow

不想你离开。 提交于 2019-12-04 05:30:13
I have been doing research on creating my own ostream and along with that a streambuf to handle the buffer for my ostream. I actually have most of it working, I can insert (<<) into my stream and get strings no problem. I do this by implimenting the virtual function xsputn. However if I input (<<) a float or an int to the stream instead of a string xsputn never gets called. I have walked through the code and I see that the stream is calling do_put, then f_put which eventually tries to put the float 1 character at a time into the buffer. I can get it to call my implementation of the virtual

How to save `std::vector<uchar>` into `std::ostream`?

泪湿孤枕 提交于 2019-12-04 05:16:50
We have created and filled some std::vector<uchar> with openCV imencode for example. Now we want to stream it for example into some http_lib which can take some sort of ostream (ostringstream) for example, or we just want to save in while we debug our programm with ofstream. So I wonder how to put std::vector<uchar> into std::ostream ? Use write : void send_data(std::ostream & o, const std::vector<uchar> & v) { o.write(reinterpret_cast<const char*>(v.data()), v.size()); } The ostream expects naked char s, but it's fine to treat uchars as those by casting that pointer. On older compilers you

operator<< overloading ostream

江枫思渺然 提交于 2019-12-04 03:27:06
问题 In order to use cout as such : std::cout << myObject, why do I have to pass an ostream object? I thought that was an implicit parameter. ostream &operator<<(ostream &out, const myClass &o) { out << o.fname << " " << o.lname; return out; } Thanks 回答1: You aren't adding another member function to ostream , since that would require redefining the class. You can't add it to myClass , since the ostream goes first. The only thing you can do is add an overload to an independent function, which is

inherit std::ostream

…衆ロ難τιáo~ 提交于 2019-12-04 01:42:23
问题 I want to define MyOStream which inherits publicly from std::ostream. Let's say I want to implement my own ofstream. How can this be done? I'll be glad for any help, coded example or any relevant link... thanks! 回答1: I don't understand exactly what you're trying to accomplish here. User code shouldn't inherit from the streams themselves, as the streams are intended to provide a generalized locale specific conversion/"stringizing" facility. If you're trying to use an ostream which can write to