iostream

c++ multiple definitions of operator<<

梦想的初衷 提交于 2019-11-28 22:21:53
问题 I am attempting to override the << operator for a class. The purpose is basically to implement a toString() like behavior for my class, so that sending it to cout will produce useful output. Using a dummy example, I have the code below. When I attempt to compile, I get the foollowing error: $ g++ main.cpp Rectangle.cpp /tmp/ccWs2n6V.o: In function `operator<<(std::basic_ostream<char, std::char_traits<char> >&, CRectangle const&)': Rectangle.cpp:(.text+0x0): multiple definition of `operator<<

How can I read line-by-line using Boost IOStreams' interface for Gzip files?

ε祈祈猫儿з 提交于 2019-11-28 21:22:22
问题 I managed to integrate the boost Iostream APIs for reading zipped files. I followed the documentation in boost page and have the following code so-far: std::stringstream outStr; ifstream file("file.gz", ios_base::in | ios_base::binary); try { boost::iostreams::filtering_istreambuf in; in.push(boost::iostreams::gzip_decompressor()); in.push(file); boost::iostreams::copy(in, outStr); } catch(const boost::iostreams::gzip_error& exception) { int error = exception.error(); if (error == boost:

ifstream: check if opened successfully

旧街凉风 提交于 2019-11-28 21:05:51
A colleague just told me that this code: std::ifstream stream(filename.c_str()); if (!stream) { throw std::runtime_error(".."); } would be wrong. He said ifstream evaluates to 0 if opening is successful. My code works, but I wanted to find the documentation but didn't see where it says how to check if opening was successful. Can you point me to it? Oliver Charlesworth operator! is overloaded for std::ifstream , so you can do this. In my opinion, though, this is a horrible abuse of operator overloading (by the standards committee). It's much more explicit what you're checking if you just do if

How to print __int128 in g++?

一个人想着一个人 提交于 2019-11-28 21:05:42
I am using the GCC built-in type __int128 for a few things in my C++ program, nothing really significant, at least not enough to justify to use BigInt library only for that and, yet, enough to prevent to remove it totally. My problem comes when I run into the printing parts my classes, here is a minimal example: #include <iostream> int main() { __int128 t = 1234567890; std::cout << t << std::endl; return t; } Commenting out the std::cout line will make this code to compile nicely with g++ , but having it will cause the following error message: int128.c: In function ‘int main()’: int128.c:7:13:

C/C++系列之复杂引用

≯℡__Kan透↙ 提交于 2019-11-28 20:41:23
以struct类型为例: 引用 #include"iostream" #include<string> using namespace std; struct mycoach { string name; int age; }; void printinfo1(mycoach &cpc) { //参数是mycoach实例的别名,指向入参的真实地址,所以一改入参的值也跟着改 cpc.name = "陈培昌"; } void main() { mycoach coach1; coach1.name = "徐晓冬"; printinfo1(coach1); cout << "这位神秘嘉宾是" << coach1.name << endl;//陈培昌 system("pause"); } 输出结果: 形参传入----可以预见这种方式不会改变struct实例的值 #include"iostream" #include<string> using namespace std; struct mycoach { string name; int age; }; void printinfo1(mycoach cpc) { //参数是mycoach实例的别名,指向入参的真实地址,所以一改入参的值也跟着改 cpc.name = "陈培昌"; } void main() { mycoach

What's the difference between while(cin) and while(cin >> num)

不问归期 提交于 2019-11-28 20:40:10
What the difference between the following two loops and When each one will stopped ? #include<iostream> #include<algorithm> #include<vector> using namespace std; int main() { int x,y; while(cin >> x){ // code } while(cin){ cin >> y; //code } return 0; } Let's look at these independently: while(cin >> x) { // code } This loop, intuitively, means "keep reading values from cin into x , and as long as a value can be read, continue looping." As soon as a value is read that isn't an int , or as soon as cin is closed, the loop terminates. This means the loop will only execute while x is valid. On the

How can I detect if a type can be streamed to an std::ostream?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 17:01:55
I'm trying to write a type trait to detect if a type has overloaded operator<<() suitable to use to an output stream. I'm missing something because I'm always getting true for a simple empty class with no operators at all. Here the code: template<typename S, typename T> class is_streamable { template<typename SS, typename TT> static auto test(SS&& s, TT&& t) -> decltype(std::forward<SS>(s) << std::forward<TT>(t)); struct dummy_t {}; static dummy_t test(...); using return_type = decltype(test(std::declval<S>(), std::declval<T>())); public: static const bool value = !std::is_same<return_type,

Converting ostream into standard string

别说谁变了你拦得住时间么 提交于 2019-11-28 16:25:53
问题 I am very new to the C++ STL, so this may be trivial. I have a ostream variable with some text in it. ostream* pout; (*pout) << "Some Text"; Is there a way to extract the stream and store it in a string of type char* ? 回答1: std::ostringstream stream; stream << "Some Text"; std::string str = stream.str(); const char* chr = str.c_str(); And I explain what's going on in the answer to this question, which I wrote not an hour ago. 回答2: The question was on ostream to string, not ostringstream to

Unexpected exception in std::ifstream

独自空忆成欢 提交于 2019-11-28 13:55:54
Experimenting with I/O I get an exception where no exception should have been thrown: #include <iostream> #include <fstream> int main() { std::ifstream f("/tmp"); std::cout << "Exception Flags: " << f.exceptions() << std::endl; if(f >> std::ws) std::cout << "This will not succeed" << std::endl; else std::cout << "Ok - it fails" << std::endl; return 0; } But the output is: Exception Flags: 0 terminate called after throwing an instance of 'std::ios_base::failure' what(): basic_filebuf::underflow error reading the file Aborted g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 Edit The test is supposed to

uint8_t iostream behavior

江枫思渺然 提交于 2019-11-28 13:19:57
Abstract: I was expecting the code: cout << uint8_t(0); to print "0", but it doesn't print anything. Long version: When I try to stream uint8_t objects to cout, I get strange characters with gcc. Is this expected behavior? Could it be that uint8_t is an alias for some char-based type? See compiler/system notes in the code example. // compile and run with: // g++ test-uint8.cpp -std=c++11 && ./a.out // -std=c++0x (for older gcc versions) /** * prints out the following with compiler: * gcc (GCC) 4.7.2 20120921 (Red Hat 4.7.2-2) * on the system: * Linux 3.7.9-101.fc17.x86_64 * Note that the first