C++ << no operator found - c++

string toString() {
std::stringstream punkte;
std::stringstream name;
std::cout << name << "hat" << punkte << "Punkte" << '\n'
return 0;
}
At this line of code. I'm receiving the error C++ << no operator found
I can't figure out what my mistake is. I have read and tried different solutions. But nothing works. Can somebody please help?
std::cout << name << "hat" << punkte << "Punkte" << '\n';
I also included this in my code:
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream, std::stringbuf
#include <fstream>

There is no overload of operator<<() that will format a std::stringstream to a std::ostream. There error does not lie.

You are trying to call operator "<<" with a stringstream parameter. In other words:
std::cout << name;
Is equivalent to:
std::cout.operator<<(name);
And that operator<<(const std::stringstream&) function doesn't exists.
I think that what you want to do is assign each stringstream their values and then print both, isn't?
string toString()
{
std::stringstream punkte;
std::stringstream name;
name << "hat";
punkte << "Punkte";
std::cout << name.str() << punkte.str() << std::endl;
return name.str();
}
Be careful with your return value, and remember that a std::stringstream is not a std::string. If you want to retrieve the std:string in the stream, you must call the str() method.

Related

How can I obtain the length of a const stringstream's buffer without copying or seeking?

I have a const std::stringstream and a desire to find out how many bytes there are in its underlying string buffer.
I cannot seekg to the end, tellg then seekg to the start again, because none of these operations are available constly.
I do not want to get the str().size() because str() returns a copy and this may not be a trivial amount of data.
Do I have any good options?
(The stream itself is presented to me as const, only because it is a member of another type, and I receive a const reference to an object of that type. The stream represents the contents of a "document", its encapsulating object represents a CGI response and I am trying to generate an accurate Content-Length HTTP header line from within operator<<(std::ostream&, const cgi_response&).)
I've never been very comfortable with stream buffers, but this seems to work for me:
#include <iostream>
#include <sstream>
std::stringstream::pos_type size_of_stream(const std::stringstream& ss)
{
std::streambuf* buf = ss.rdbuf();
// Get the current position so we can restore it later
std::stringstream::pos_type original = buf->pubseekoff(0, ss.cur, ss.out);
// Seek to end and get the position
std::stringstream::pos_type end = buf->pubseekoff(0, ss.end, ss.out);
// Restore the position
buf->pubseekpos(original, ss.out);
return end;
}
int main()
{
std::stringstream ss;
ss << "Hello";
ss << ' ';
ss << "World";
ss << 42;
std::cout << size_of_stream(ss) << std::endl;
// Make sure the output string is still the same
ss << "\nnew line";
std::cout << ss.str() << std::endl;
std::string str;
ss >> str;
std::cout << str << std::endl;
}
The key is that rdbuf() is const but returns a non-const buffer, which can then be used to seek.
If you want to know the remaining available input size:
#include <iostream>
#include <sstream>
std::size_t input_available(const std::stringstream& s)
{
std::streambuf* buf = s.rdbuf();
std::streampos pos = buf->pubseekoff(0, std::ios_base::cur, std::ios_base::in);
std::streampos end = buf->pubseekoff(0, std::ios_base::end, std::ios_base::in);
buf->pubseekpos(pos, std::ios_base::in);
return end - pos;
}
int main()
{
std::stringstream stream;
// Output
std::cout << input_available(stream) << std::endl; // 0
stream << "123 ";
std::cout << input_available(stream) << std::endl; // 4
stream << "567";
std::cout << input_available(stream) << std::endl; // 7
// Input
std::string s;
stream >> s;
std::cout << input_available(stream) << std::endl; // 4
stream >> s;
std::cout << input_available(stream) << std::endl; // 0
}
This is similar to #Cornstalks solution, but positions the input sequence correctly.
This should work :))
#include <iostream>
#include <sstream>
#include <boost/move/move.hpp>
int main()
{
const std::stringstream ss("hello");
std::cout << boost::move(ss).str().size();
}

how to print Ascii code of a string in c++

I have a string and I want to print hex value of each parts ascii code.
for example if the string is "0200" the output will be 30323030 .
and here's my code:
string bit_pattern;
bit_pattern = "5678008180000000";
cout << hex << bit_pattern;
but it prints 5678008180000000 instead of 35363738303038313830303030303030
how do i fix it???
You can use the following
for (int i=0; i<bit_pattern.length(); i++)
cout << hex << (int)bit_pattern[i];
to print the ascii value (in hex format) char by char.
You're just sending the same std::string right to std::cout. Just sending the hex manipulator isn't going to magically convert all those chars.
I admit this is complete overkill, but I was bored:
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
class ascicodes
{
std::ostringstream ss;
public:
friend std::ostream& operator <<(std::ostream& os, const ascicodes& obj)
{
os << obj.ss.str();
return os;
}
ascicodes(const std::string& s)
{
ss << std::hex << std::setfill('0');
std::for_each(s.begin(), s.end(),
[this](char ch)
{
ss << std::setw(2) << static_cast<unsigned int>(ch);
});
}
};
int main()
{
std::string bit_pattern = "5678008180000000";
std::cout << ascicodes(bit_pattern) << std::endl;
std::cout << ascicodes("A completely different string") << std::endl;
return 0;
}
Output
35363738303038313830303030303030
4120636f6d706c6574656c7920646966666572656e7420737472696e67

error: reference to non-static member function must be called

I'm working on a program that collects family last names and then asks for names of first name of family members. I'm using an istringstream object to separate the names collected from a getline. For the step where I try to fill the elements in the map, I get the following error. I know that you can create keys by supplying them in brackets following the name of the map. If that method is valid, why do I get the following error.
ex11_14.cpp:19:6: error: reference to non-static member function must be called
family[lname].push_back[fname];
Code:
#include <iostream>
#include <map>
#include <vector>
#include <sstream>
int main()
{
std::string lname, fname;
std::string last, children;
std::map<std::string, std::vector<std::string>> family;
std::cout << "Enter family names" << std::endl;
getline(std::cin, last);
std::istringstream i{last};
std::cout << "Enter children's names" << std::endl;
while(i >> lname) {
std::cout << lname << std::endl;
getline(std::cin, children);
std::istringstream j{children};
while(j >> fname)
family[lname].push_back[fname];
}
for(auto &c:family) {
std::cout << c.first << std::endl;
for(auto &w:c.second)
std::cout << w << " ";
std::cout << std::endl;
}
std::cout << std::endl;
return 0;
}
Sorry. push_back should be called with () not []. Fixes the issue.
It seems, this line:
family[lname].push_back[fname];
wants to read
family[lname].push_back(fname);
BTW, don't use std::endl. Use '\n' to get a newline and std::flush to flush the stream: use of std::endl is most often a performance problem without being useful in the first place.

ostringstream breaks cout?

I want to output the content of a ostringstream to some other stream (for example std::cout). I know that I can use std::ostringstream::str() but I assume it has an overhead on copying the stream contents to a string and then further to the other stream. I found that I could use std::ostringstream::rdbuf() (Comment suggesting that has 25 votes). But it breaks std::cout as is shown in the output of the test program below. Am I doing something wrong?
#include <iostream>
#include <sstream>
int main(int argc, char const *argv[])
{
std::ostringstream ss;
ss << "some data" << std::endl;
std::cerr << std::cout << std::endl;
std::cout << "start" << std::endl;
std::cout << ss.str();
std::cout << ss.rdbuf();
std::cout << "end" << std::endl;
std::cerr << std::cout << std::endl;
return 0;
}
Results in:
0x6013b8
start
some data
0
Your problem is that the rdbuf() buffer for an ostringstream is, as you might expect from the name, write only (the ostringstream returns the string through the str() method). You can't read the data back out of it through the buffer pointer.
Change your ostringstream to stringstream and it should work fine.

C++: something is getting sent to the console: how do I, instead, store it to a variable?

string convert_binary_to_hex(string binary_value)
{
bitset<8> set(binary_value);
cout << hex << set.to_ulong() << endl; // this is the output that I want to store to a variable to return
return "";
}
I've not really done C before. =D
EDIT:
a user suggested ostringstream:
ostringstream result;
bitset<8> set(binary_value);
result << hex << set.to_ulong() << endl;
return result.str();
but it now give this error:
main.cpp:20: error: aggregate ‘std::ostringstream result’ has incomplete type and cannot be defined
Also, my imports:
#include <stdio.h>
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
Write it to an instance of std::ostringstream:
string convert_binary_to_hex(string binary_value)
{
bitset<8> set(binary_value);
ostringstream oss;
oss << hex << set.to_ulong() << endl; // this is the output that I want to store to a variable to return
return oss.str();
}
See this answer:
C++ equivalent of sprintf?
And use the out variable like you're using cout here, and instead of
std::cout << out.str() << '\n';
you would just do
return out.str();