Questions tagged [ostream]
In C++ std::ostream is the base class for output streams.
754
questions
0
votes
0
answers
50
views
Explicitly deleted functions
we are migrating code to VS 2019 from legacy code
We have an overloaded function on operator '<<' that is invoking basic_ostream function
when executing the following lines.
CStringArray asLine;
...
0
votes
1
answer
56
views
How do I call a function with an std::ostream& argument using cppyy?
I am using cppyy for a native c++ project. I have a function that takes in a std::ostream& object and I am not sure how to create something that calls it from python.
I have tried doing
import io
...
0
votes
0
answers
49
views
Did handling of "operator<<" in g++ change between v4.8.5 and v8.4.1?
I have a large code that compiled fine in g++ 4.8.5 (on RHEL7) but fails in 8.4.1 (on RHEL8) (I do not have any intermediate g++ versions currently available). The compiling fails in a certain file ...
0
votes
0
answers
47
views
Visual Studio doesn't show output when using wxWidgets
I tried to print a debug message using cout but I am not getting any output although my app is running.
I am using the IMPLEMENT_APP macro.
Here is the code I tried to print some information:
std::...
0
votes
0
answers
48
views
How to create a custom std::ostream which has an override for each primitive type
I am creating a custom logger that I want to use as a drop-in replacement for a std::ostream. I want to have overrides for each primitive type -- I do not want to implement a char-based streambuf ...
-1
votes
1
answer
38
views
c++ overloaded << prints adress of object attribute, instead of actual value
So I am learning operation overloading.
I am trying to overload << to print object properties.
In my case Properties is a class that inherits the public section of the person class.
This is what ...
-1
votes
1
answer
59
views
Writing to the stream buffer of a string stream overrides the previous data
What I want to do is to create string stream , and output stream, giving the buffer of string stream to output stream, so that it will output data to the string stream. Everything seems fine, but ...
0
votes
0
answers
28
views
Ofstream write stopped set ios::fail while trying to write to stream with ios::eof set since clang15 and gcc12
Consider the following code:
#include <iostream>
#include <ostream>
#include <fstream>
int main() {
std::ofstream f("tmp");
f.setstate(std::ios_base::...
2
votes
1
answer
73
views
Is there a way to check if a fmt::output_file has been opened successfully for writing
I'm trying to use fmt::output_file and am looking for a way to check if the file has been opened successfully for writing.
Is this possible? What's the idiomatic way to do this?
Demo
0
votes
0
answers
29
views
operator<< overload as a member function, why does it not work? [duplicate]
#include <iostream>
class A
{
private:
int x;
public:
A(int x) : x{x}
{
std::cout << "new A object !!!\n";
}
std::ostream& operator<< (std:...
1
vote
2
answers
77
views
Wierd stuff happens when overloading operator<< with a class template
Here's the functionality I am expecting to achieve:
darray<int> a;
a.push_back(1);
a.push_back(2);
a.push_back(3);
std::cout << a << std::endl; // displays: {1, 2, 3}
My ...
0
votes
1
answer
304
views
Error C++: no operator matches these operands. operand types are: std::ostream << void [duplicate]
I'm getting the error in my GetInfo() method:
void Quadrangle::GetInfo() {
cout << "Area = " << GetArea() << endl;
cout << GetPerimeter() << endl; //!!!...
0
votes
0
answers
23
views
Move Assignment Operator for a Custom Filtering Streambuf Class
I have written a "filtering streambuf" class (FilterStreambuf below). Only stream output is considered. The end goal is a std::ostream object which filters, writes to a std::ofstream, and ...
1
vote
1
answer
175
views
Is ostream& operator<< better practice in a class than using std::cout?
In a Linked List class, I created a display method to print out the Linked List as it is currently formulated like so:
void LinkedList::display() {
Node* curr = m_head;
while (curr) {
std:...
0
votes
1
answer
85
views
C++ stringstream question, how can I make each line seperate
I'm sorry the title may be inaccurate.I'm new to C++.
Here is my code and output...
#include <iostream>
#include <sstream>
using namespace std;
class LogLine {
private:
stringstream ss;...
-1
votes
1
answer
47
views
Why does the first element in a map get displayed last? [duplicate]
I am attempting to write a general utility code file that I can include whenever I want to for debugging purposes, instead of having to write the same code over and over again. Among those utility ...
-2
votes
1
answer
52
views
How to read/write to a file in C++?
I'm new to file read/write in c++. Please someone help me the best way to read the file somewhat like shown below ta class like this
class Student
{
public:
string fName;
string sName;
int mark
};
//...
0
votes
1
answer
77
views
How to prevent the compiler from checking the syntactic correctness of a certain branch
I basically want to select one of the branches at compile-time but can't figure out how to address the error that shows up.
Here is the code (link):
#include <iostream>
#include <vector>
#...
0
votes
1
answer
35
views
Overloading of operator<< requiring const input argument for compilation
While compiling the below standard-code of overloaded ostream operator, a compilation error is being returned for the post-fix increment console-out case.
error: no match for 'operator<<' (...
0
votes
0
answers
104
views
The best std::ostream to get bytes from stream - Windows
On Windows, I am using TIFFStreamOpen(const char*, std::ostream *); from libtiff and passing std::ostringstream to it for getting data. But I think that is not the fastest way to get bytes, because ...
1
vote
2
answers
187
views
C++ console output not switching to a new line when the program ends [duplicate]
So I'm just starting to learn C++, and I'm just trying to get everything set up. When I run the compiled binary for my hello world code, it display's the console output, but doesn't switch to a new ...
1
vote
3
answers
84
views
How to write binary into a file using std::ostream::put?
I'm trying to implement TGA-Compression using run-length-encoding algorithm. One problem that I'm having is outputting the bytes, that give information how many times a color is repeated, into a file. ...
0
votes
1
answer
58
views
Operator overload for ostream not working with user defined class [duplicate]
I have this simple program and when i try to cout << 75.0_stC ; i have multiple errors and i don't know why.This things only happen when i pass my temperature object via reference.
class ...
0
votes
0
answers
34
views
Eclipse CDT and Visual Studio Code have different i/o behavior with the same code why is this? [duplicate]
//the code below from a tutorial is demonstrating the usefulness
//of the function getline() when entering strings with whitespaces.
#include <string>
#include <iostream>
int main()
{
...
0
votes
0
answers
35
views
Is there any solution to transfer data to any stream in C++? [duplicate]
Summary
I'm implementing a Data Structure from scratch without using templates by the moment, all I have is a simple prototype of a Vector.
Part of the operations of that DS is a method called print ...
1
vote
1
answer
270
views
Can I use an std::ostream to print into a given buffer?
I have a given buffer of characters (maybe even the buffer backing an std::string, but never mind that). I want to print into that buffer using an std::ostream, i.e. I want to be able to write:
span&...
1
vote
3
answers
119
views
Cant cout item in container with std::any
This script
#include <iostream>
#include <unordered_map>
#include <any>
using namespace std;
int main() {
unordered_map<int, any> test;
test[5] = "Hey!";
...
-1
votes
1
answer
45
views
Is there a way to use (std::cin).get() to accept newlines when asking for input?
(std::cin).get()
I want to use std::cin to collect a string with spaces, like "1/2 oz of flower". When I add a space and then press enter it exits the program instead of collecting the rest ...
0
votes
0
answers
25
views
How do I overload the << operators for my vector class? [duplicate]
So this is my own Vector class, which I have written:
#ifndef VEKTOR_HPP
#define VEKTOR_HPP
#include <iostream>
template<typename T>
class Vektor
{
T* tomb;
size_t meret;
...
0
votes
2
answers
57
views
`operator<<` on a container which works for both values and references
There is a container class container which is templated, so it can contain anything.
I want to add ability to print its contents to std::ostream so I've overriden operator<<.
However this has a ...
0
votes
1
answer
236
views
std::ostream class member variable to be used as std::ofstream or std::ostringstream
I'm struggling with streams. I would like to have FileWriter that writes MyObject data to file or to a stringstream. I don't want the interface of write() to take the file path or the stringstream ...
-1
votes
1
answer
483
views
Overloaded 'operator<<' must be a binary operator (has 3 parameters)
I am trying to implement Heap ADT in C++.
I am currently facing a problem in the my overloaded operator<<. I saw many solutions but non worked for me: first solution || Here is the second one
...
0
votes
0
answers
88
views
How do you catch the output of a void function that takes ostream as a parameter into a stringstream?
Let's say we have:
void print(std::ostream &o) {
o << "Hello World!";
}
and:
int main() {
std::stringstream ss;
ss << print(std::cout); // does not work
std::...
0
votes
2
answers
91
views
Creating a sticky manipulator that inserts a delimiter between each token of an output stream
I've seen examples that allow you to create a manipulator that inserts delimiters but none of those manipulators are sticky. That is, the manipulator returns a special class that inserts the delimiter,...
2
votes
1
answer
169
views
ofstream::write writes zeros for part of the file
I would like to write the contents of a vector< int> to a binary file. This current program is supposed to save the integers 0 to 99 in the file, but it only saves the first 26 integers.
std::...
1
vote
2
answers
464
views
How to create customized ostream / streambuf classes for flexible output stream?
I found quite some info on output redirection, creation of streambuffers and ostream classes, but I did not manage to apply this succesfully yet for my purpose. This post has become quite lengthy ...
0
votes
1
answer
303
views
error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
So, I have recently approched OOP in c++ through university and I found myself with a few problems.
I gave a shot at overloading the ostream operator << and found myself with some problems to ...
1
vote
1
answer
56
views
ostream << operator not getting invoked
I have created a class Animal with some basic properties and added a no data constructor.
I have overloaded the ostream operator as well to print the properties.
Animal.cpp
#include<bits/stdc++.h&...
0
votes
3
answers
122
views
Can we overload operator<< with the first parameter being of the type std::ostream&& instead of std::ostream&
I have learnt that we can overload operator<< as shown below :
class Person
{
public:
friend std::ostream& operator<<(std::ostream& os, const Person& obj);
};
I ...
0
votes
1
answer
193
views
Writing contents of an STL Map to output stream using ostream_iterator
I have a map<string, int> object and I want to use ostream_iterator to write the contents of it to the screen or a file. I have overloaded output operator (operator<<) so that it can be ...
0
votes
0
answers
14
views
What does the fstream binary flag do [duplicate]
I have been experimenting with file streams lately but I came across the std::binary flag to open the file in "binary mode" and I can't seem to find what that actually means.
What is "...
0
votes
2
answers
385
views
Overloading ostream << operator for a class with private key member
I am trying to overload the ostream << operator for class List
class Node
{
public:
int data;
Node *next;
};
class List
{
private:
Node *head;
public:
List() : head(NULL) {}
...
0
votes
2
answers
113
views
make std::ostream automatically ident when encountering special characters
I would like to have some facility which makes a std::ostream (or derived) automatically ident on encountering special characters (or special objects). Let's assume that the special characters are <...
0
votes
1
answer
57
views
Using functions while overloading an operator
I'm currently working on a game to brush up my C++ skills and I'm trying to use more advanced concepts in order to learn more about the language itself.
I'm creating a deck of cards using an enum to ...
0
votes
1
answer
146
views
differences between std::for_each and std::copy, std::ostream_iterator<T> when printing a vector
Recently, I have come across code that prints a vector like so
std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(std::cout, " ");
comparing that to what I am used to (...
0
votes
0
answers
219
views
std::ostream pointer initialization
My program crashes without any error, so I'm asking for advice on this new topic I'm not familiar with.
Header file:
class CANLog
{
public:
CANLog( std::string file_name );
CANLog( std::...
1
vote
2
answers
216
views
Friend Function cannot access private members in C++
I have created class and trying to overload ostream operator using a friend function but my friend is not able access private members of functions. Please help me figure out the problem. I have ...
-2
votes
4
answers
512
views
cout operator << doesn't work for vector<char> [duplicate]
Why doesn't this vector print out?
void str_read(std::vector<char> str);
int main() {
std::vector<char> str;
str_read(str);
std::cout << str << std::endl;
...
0
votes
1
answer
30
views
C++ unexpected output when using operator overloading
I am attempting to go through a "Circle" structure (basically a binary tree). Each circle has an centerX, centerY, radius, and two leaf nodes. These leaves will either both be null or both ...
0
votes
1
answer
35
views
Why can use << in const function body? [duplicate]
const function body can't change members of class.The operator << will change the ostream object, if the ostream object is a member.
this is example in C++ primer(5th edition):
class PrintString{...