Menu
  • HOME
  • TAGS

Aren't the words ostream and istream switched in the text below?

c++,c++11,ostream,istream

No. ostream "outputs" from arbitrary types to character sequences, and istream "inputs" to arbitrary types from character sequences, just as described.

Create istream and ostream objects in C++

c++,object,iostream,ostream

Yes, you can create any stream you want. Since a stream is a flow of data with a source and a sink, you typically want to use either of the following: std::stringstream - add data yourself, access it in stream form std::{i,o}fstream - data comes from / goes to a...

C++: Error outputting a custom hash key value from unordered_map to std::cout

c++,hash,unordered-map,ostream

As far as I am aware, there is no standard interface for exposing the hash value (as opposed to the hashing function) from std::unordered_map. As you have seen, dereferencing a std::unordered_map<Key,V>::iterator yields something convertible to std::unordered_map<Key,V>::value_type, which in turn is a std::pair<const Key,V> representing a (key,value) pair, rather than a...

declare template class within class with same types as parent class

c++,templates,namespaces,ostream

It should be: template <typename V, typename W> std::ostream &operator<< (std::ostream& os, typename Foo<V,W>::template Bar<V,W> &b) {return os;} but changing the class may be simpler: template <typename X, typename Y> class Foo { public: class Bar { X a; Y b; }; Bar baba; }; template <typename V, typename W>...

Overloading ostream << operator returning address

c++,operator-overloading,ostream

if you want your object to work in both cases cout << p ; cout << *p; you have to overload your operator for pointers also, so you can add this to your code and keep the old one: friend ostream& operator<<(ostream& os, const Person* p); ostream& operator<<(ostream& os, const...

custom ostream that will call the function with string as argument

c++,iostream,ostream

From the description it seems you just want to build a string and process this. You can do so using string streams: #include <iostream> #include <sstream> void f(std::ostream& out) { out << "f()"; } int main() { std::ostringstream out; f(out); std::cout << "string='" << out.str() << "'\n"; f(std::cout); } If...

C++ ambigous overload for generic template ostream << operator

c++,templates,c++11,ostream,enable-if

You get an ambiguous overload for 'operator<< error when the type is std::string, because the templated version in your code has an equal precedence with the one shipped in the ostream header. You can check that this is the source of your problem by changing your test program with this:...

Replace line in txt file c++

c++,fstream,ostream

Use ReadLine and find the line you wanna replace, and use replace to replace the thing you wanna replace. For example write: string Example = "Text to find"; openFile="C:\\accounts.txt"; // the path of the file ReadFile(openFile, Example); OR #include <fstream> #include <iostream> #include <string> int main() { ifstream openFile; string...

std::iostream read or write with count zero and invalid buffer

c++,c++11,iostream,ostream,istream

Here is what the standard says about std::basic_istream<...>::read() in 27.7.2.3 [istream.unformatted] paragraphs 30 and 31 (emphasis is mine): basic_istream<charT,traits>& read(char_type* s, streamsize n); Effects: Behaves as an unformatted input function (as described in 27.7.2.3, paragraph 1). After constructing a sentry object, if !good() calls setstate(failbit) which may throw an exception,...

ostream operator<< not working properly

c++,arrays,printing,hashtable,ostream

There is a logic error in the function Customer::getHash. That may not solve your problem but it should be fixed anyway. int Customer::getHash(int hash) { string key = getLastname(); cout<<"key: "<<key<<endl; // getFirstname(); // getID(); int i = 0; // int j = 0; // int k = 0; for...

How to convert a vector to string in C++ [closed]

c++,string,vector,iterator,ostream

That code is only needed if you want to add a delimiter after every inserted integer, but even then it doesn't have to be that complicated. A simple loop and the use of to_string is far more readable: std::string str; for (int i = 0; i < vec.size(); ++i) {...

Why I cannot put this operator overload in the same namespace as the struct?

c++,operator-overloading,ostream

In Argument Depended Lookup (or Koenig Lookup), compiler adds to the scope of visibility all symbols declared in parent scopes of each parameter. Even if Y is "child namespace" of X, they are not related in terms of ADL. First of your parameters is type defined in std:: namespace, while...

Streams, chars, and newlines

c++,newline,ostream

use getline() get every line int the char *

Strange crash with C++ atexit() function

c++,crash,cout,ostream,atexit

Seems like my suspicions turned out to be true. I modified the code like so: #include <iostream> using namespace std; #include <Windows.h> void crash() { printf("%i\n", GetCurrentThreadId()); system("PAUSE"); } int main() { printf("%i\n", GetCurrentThreadId()); atexit(crash); //while(true); return 0; } When the program exists normally both printf()s display identical thread IDs,...

Cannot write across a Linux pipe with istream and ostream pointers

c++,linux,pipe,ostream,istream

Victory! Thanks Barmar! There are two problems with the code above; the first one is embarrassing. The writer complaints go into a logfile, and I totally looked in the wrong logfile. The correct logfile was full of complaints. I can't use tellp on a pipe stream. So, it works if...

Writing the contents of a map through operator overloading

c++,dictionary,containers,overloading,ostream

Your operator<< for stack is popping all of the elements off of the stack, so the stack is empty by the time you make a copy of it to put in the map. To demonstrate, if I change your main() to this: int main() { stack<int> s2; s2.push(1); s2.push(2); cout...

How cout is for static duration?

c++,stream,cout,outputstream,ostream

[basic.stc.static]/1: All variables which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these entities shall last for the duration of the program ...

C++ Last Number is only 2bytes instead of 4 when writing integer to fstream

c++,byte,fstream,ostream

You are using the operator<<(ostream&, const char *) overload for something that is plainly not a C-style null-terminated string, which results in undefined behavior.1 In the case of 40 2C 00 00, the function interpreted the zero-byte as a null terminator, so it only printed the first two bytes. When...

fstream does not function in Qt Creator

c++,qt,fstream,ifstream,ostream

You are passing a std::string to the std::ofstream constructor. This is a C++11 feature and to use this you need to pass -std=c++11 to GCC or Clang. MSVC automatically compiles for its hybrid not-quite-C++11-or-anything-else language that the compiler release compiles. If you're using Qt 5's qmake, you can do just...

Imitating fortran print and write syntaxes in C++

c++,fstream,ostream

fooprint(out) is not the creation of a temporary but rather the declaration of a variable of type fooprint with the name provided as the argument to the macro. In order to not make this a declaration, and instead an expression, two quick changes you can make are surrounding it in...

c++ errors with ostream

c++,ostream

since nobody is posting correct answer to show that this has been solved Your function void display(std::ostream& os) const; should return something to print out; for example char * char * display(std::ostream& os) const; when using << operator, the function you are using toStrWithStyle should return something to be printed...

Using std::ostream as argument to a print function

c++,iostream,ostream

The benefit of switching to the ostream version is that in the case you later need to print to other places besides std::cout then you can do it with the same function implementation, whereas in this moment if you want to print to a file you would need to use...

Redefinition error in ostream overload in template and inherited classes

c++,templates,inheritance,ostream

Replace template <type S> friend std::ostream& operator<<(std::ostream& out, const derived<S>& D) { return (D.print(out)); } with friend std::ostream& operator<<(std::ostream& out, const derived<T>& D) { return (D.print(out)); } error will go away. With earlier definition, you are trying to define a new template function with same signature....

why is std::cout convertible to void* if using g++?

c++,pointers,implicit-conversion,ostream

Read this (your question is answered in the very last section, "The safe bool problem"). To elaborate a bit, the implementation defines an implicit conversion to void* defined for things like std::cin and std::cout, just so that code like while(std::cin>>x){...} compiles, while code like int x = std::cin; doesn't. It's...

Overloading ostream operator (<<) for class defined within a namespace

c++,namespaces,operator-overloading,ostream

Question: is this the correct way of solving this problem? Answer: Yes, it is the correct way of solving the problem. Question: Isn't this approach giving away details of the implementation? Answer: Not at all. The line friend std::ostream& operator <<(std::ostream& output, const Bar&); declares the function as an external...

How to display attributes of an object using ostream_iterator.?

c++,ostream

You need to overload operator<< for your class, because that's what ostream_iterator calls when being assigned to. Something like this: std::ostream& operator<<(std::ostream& os, const Student& s) { return os << get_name() << get_age() << get_marks(); // needs some formatting } And then you construct the iterator with Student as template...

Deleting from a file C++

c++,fstream,ostream

You have three options: 1) Output the modified data to a new file; or 2) Mark records as deleted, but available for reuse; or 3) Move data "up" to the unused slots (like an array). Maybe you should be using a database instead. Many applications write the original and modified...

Overloading fstream << operator to save “any” kind of data

c++,save,operator-overloading,ostream

You are streaming Test by non-const reference: friend std::ostream& operator << (std::ostream& os, Test& p); You want to stream it by const reference: friend std::ostream& operator << (std::ostream& os, const Test& p); ^^^^^^ The error comes from the fact that when you call it from save(), you are passing in...

Connecting two streaming functions c++

c++,stream,iostream,ostream,istream

It seems, you want to create something that channels the data written to Get()'s std::ostream to Put()'s std::istream. To do so you'll need to write a suitable stream which may need to deal with connecting multiple threads. If it is sufficient to read data from the stream written to by...

Does the return type of the C++ stream insertion operator need to be an std::ostream?

c++,operator-overloading,ostream

Yes, it's perfectly legal to overload operator<< while varying the return type - it just won't work properly for the common use of streaming your type alongside other types to C++ streams. As an example... Foo my_foo; std::cout << my_foo << '\n'; ...will have evaluation attempted... operator<<(std::cout, my_foo).operator<<(std::ostream& ??? '\n'...

Overloading operator<<(ostream&, T) where T is “enum class MyEnum”

c++,c++11,operator-overloading,ostream

std::ostream is an alias for std::basic_ostream<char>. std::wcout is of type std::basic_ostream<wchar_t>, also known as std::wostream. Note the different character types. If you want your operator<< to work with std::wcout, it should take and return std::wostream& rather than std::ostream&....

Modify contents of basic_ostream object in c++ or, deleting data contents of basic_ostream object

c++,stream,ostream

You can try to change the current position in the stream using ostream::seekp() or streambuf::pubseekpos(): std::ofstream out; out.seekp(-10, std::ios_base::cur); // move back 10 positions from current location out.rdbuf()->pubseekpos(12); // move to position 12 Keep in mind that: this might fail if the underlying destination of the data does not support...

C++ cout with prefix

c++,iostream,cout,ostream

The way you modify the behavior of std::ostream is not by overloading any of the output operators! Instead, you derive a class from std::streambuf and override the virtual functions overflow() and sync(). In you case you'd probably create a filtering stream buffer, i.e., you'd take another std::streambuf as argument and...

Error C2679: binary '<<' : no operator found (vector iterators used)

c++,operator-overloading,operator-keyword,cout,ostream

You must overload operator<< between an ostream object and your own object. ostream &operator<<(ostream &output, const projects &prj) { //print something you want of prj with output << ... return output; } Also, you need to add this friend definition to your class if the overloaded operator needs to access...

Moving in a .txt

c++,stream,ostream

You can use ostream::seekp to move the stream position backward or forward, as described here

C++ avoiding newline with cout <<

c++,io,ostream

You cannot fix this without modifying BaseEl::display() to stop producing new line at the end of the output. In general, it is a bad idea to add endl to your own output. Let the caller do that if he needs a newline. Note that a more C++-like approach to output...