I mistyped the error message before. It is fixed now.
I'm currently getting the following compiler error message
error: no match for 'operator<<' in 'std::cout << Collection::operator[](int)(j)'
The code that the compiler is complaining about is
cout << testingSet[j];
Where testingSet is an object of type Collection that has operator[] overloaded to return an object of type Example. Example has a friend function that overloads operator<< for ostream and Example.
note: this actually compiles just fine within Visual Studio; however does not compile using g++.
Here is the implementation of operator<<:
ostream& operator<<(ostream &strm, Example &ex)
{
strm << endl << endl;
strm << "{ ";
map<string, string>::iterator attrib;
for(attrib = ex.attributes.begin(); attrib != ex.attributes.end(); ++attrib)
{
strm << "(" << attrib->first << " = " << attrib->second << "), ";
}
return strm << "} classification = " << (ex.classification ? "true" : "false") << endl;
}
And the operator[]
Example Collection::operator[](int i)
{
return examples[i];
}
Probably your operator should be declared as:
ostream& operator<<(ostream &strm, const Example &ex)
Note the const-reference to Example.
Visual Studio has an extension that allows to bind a reference to a non-const r-value. My guess is that your operator[] returns an r-value.
Anyway, an operator<< should be const as it is not expected to modify the written object.
Related
I have a c++11 code which has a function like the following:
bool prepareSwitch(const std::list<hardware_interface::ControllerInfo>& start_list,
const std::list<hardware_interface::ControllerInfo>& stop_list)
for debugging this code, I'd like to investigate/print for example the start_list, however, having a quite basic experience in c++ has made this confusing for me. Yet, the additional information that I can have about this input argument is about <hardware_interface::ControllerInfo> the described in this documentation.
I'm not sure how to use this information to print or investigate this input. Can someone guide a little about it?
using std::cout << start_list << std::endl; gave a compilation error.
To ease debugging you can overload the following operators. (adapt the formatting as needed)
std::ostream & operator<<(std::ostream & os, const hardware_interface::InterfaceResources &ir)
{
os << "hi: " << ir.hardware_interface << std::endl;
for (const auto & r : ir.resources)
{
os << "r: "<< r << std:;endl;
}
return os
}
std::ostream & operator<<(std::ostream & os, const hardware_interface::ControllerInfo &ci)
{
os << "name:" << ci.name << std::endl;
os << "type: " << ci.type<< std::endl;
for (const auto & cr : ci.claimed_resources)
{
os << "cr: " << cr << std::endl;
}
return os
}
Then you can print your functions arguments:
for (const auto& ci: startList)
{
std::cout << ci;
}
While compiling the below code, I am getting an error:
Expression.h
class Expression{
...
protected:
std::ostream Os;
};
Expression.c
Expression::Expression() : Os(std::cout)
{
...
}
Expression::Expression(std::ofstream &os) : Os(os)
{
...
}
Expression::Dump()
{
Os << "=============================================================" << std::endl;
Os << "Os: " << Os << std::endl;
}
error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'std::ostream {aka std::basic_ostream<char>}')
What is my mistake? What should I do to fix it?
And while giving initial value to parameter like this Os(std::cout), what does it mean?
Consider
Os << "Os: " << Os << std::endl;
Let us unpack it into its full functional glory:
std::ostream& t1 = Os.operator <<("Os: ");
std::ostream& t2 = t1.operator <<(Os); // << Here
std::ostream& t3 = t2.operator <<(std::endl);
The problem is the line marked "Here". You are streaming an ostream to itself. This is nonsensical. What are you trying to do?
If you are trying to output an identifier for the stream, you should use the address:
Os << "Os: " << &Os << std::endl;
If you are trying to output the contents of the stream to stdout, you need to make Os be an ostringstream, and then output the string buffer:
std::ostringstream Os;
...
std::stdout << "Os: " << Os.str() << std::endl;
Note for the pedantic: I believe my breakdown of the original line into function calls and initializations is actually identical in this particular case (because there are no temporary objects - only references); in general, the lifetime of temporaries might be subtly different.
The error is because there is no standard operator<< that writes an ostream to another ostream.
Also, ostream can't be copy-constructed from another ostream, so if you are trying to specify an output ostream for Expression to write to then your Os member needs to be declared as a reference instead.
class Expression {
...
protected:
std::ostream &Os;
};
Expression::Expression() : Os(std::cout)
{
...
}
Expression::Expression(std::ofstream &os) : Os(os)
{
...
}
Expression::Dump()
{
Os << "=============================================================" << std::endl;
Os << "Os: " << SomethingThatIsNotOs << std::endl;
}
Hi all I have this code -
typedef struct signal_declairation{
//these are for 2001 type declairation
bool is_array;
int array_start, array_end;
PORT_ATTRIBUTE port_attribute; //port direction
string port_type; //logic, UDT, wire, accept every thing
//this is common to 2001 and normal
string port_name;
//dumper method
friend ostream& operator<<(ostream &out, signal_declairation &m_signal_declairation);
} SIGNAL_DECLAIRATION;
std::ostream& operator<< (ostream &out, signal_declairation &m_signal_declairation)
{
string port_direction;
switch(m_signal_declairation.port_attribute){
case INPUT: port_direction = "input";
break;
case OUTPUT: port_direction = "output";
break;
case INOUT: port_direction = "inout";
break;
}
if(m_signal_declairation.is_array==true){
out << " Port Name = " << m_signal_declairation.port_name
<< " Port Direction = " << m_signal_declairation.port_attribute
<< " Port type = " << m_signal_declairation.port_type
<< " This is an arrayed port :: Array Start = " << m_signal_declairation.array_start
<< " Array End = " << m_signal_declairation.array_end ;
}else{
out << " Port Name = " << m_signal_declairation.port_name
<< " Port Direction = " << m_signal_declairation.port_attribute
<< " Port type = " << m_signal_declairation.port_type;
}
return out;
}
When I try and compile, I get errors that look like this -
parser_globals.h: In function ‘std::ostream& operator<<(std::ostream&,
signal_declairation&)’: parser_globals.h:53:9: error: no match for
‘operator<<’ (operand types are ‘std::ostream {aka
std::basic_ostream}’ and ‘const char [14]’)
out << " Port Name = " << m_signal_declairation.port_name
Not sure why I am not able to pass class members to << as shown above. Can anyone help ????
Thanks
Raj
If you had provided the complete error messages that you get and not just a small snippet, things would have been easier.
Based on what you provided so far, it looks like you might have forgot to include the necessary header(s) for the standard streams, e.g.:
#include <iostream>
This would cause the compiler to not find the operator<< overloads for the base types or the types from the standard library.
change your overload definition :
std::ostream& operator<< (ostream &out, signal_declairation &m_signal_declairation)
by std::ostream& operator<< (ostream &out,const signal_declairation &m_signal_declairation) .
Dont forget the prototype too.
The compiler complains about the ‘const char [14]’) out << " Port Name = ". It looks like the compiler cannot see the " Port Name - " as string, rather it sees it as character array. Have you include ?
This question already has answers here:
no match for ‘operator<<’ in ‘std::operator
(6 answers)
Closed 5 years ago.
I am developing gsoap web service where I am retrieving vectors of objects in return of a query. I have two ways to do it: first by simple loop and by iterator. None of them working.
The error is:
error: no match for 'operator<<' in 'std::cout
mPer.MultiplePersons::info.std::vector<_Tp, _Alloc>::at<PersonInfo, std::allocator<PersonInfo> >(((std::vector<PersonInfo>::size_type)i))'
MultiplePersons mPer; // Multiple Person is a class, containing vector<PersonInfo> info
std::vector<PersonInfo>info; // PersonInfo is class having attributes Name, sex, etc.
std::vector<PersonInfo>::iterator it;
cout << "First Name: \t";
cin >> firstname;
if (p.idenGetFirstName(firstname, &mPer) == SOAP_OK) {
// for (int i = 0; i < mPer.info.size(); i++) {
// cout << mPer.info.at(i); //Error
//}
for (it = info.begin(); it != info.end(); ++it) {
cout << *it; // Error
}
} else p.soap_stream_fault(std::cerr);
}
It's obvious that operator overloading operator<< in cout is the problem. I have looked at several problems related to this, but no one helped me out. If someone can provide a concrete example on how to solve it, it would be very appreciated. (Please do not talk in general about it, I am new to C++ and I have spent three days on it searching for solution.)
You need to provide an output stream operator for PersonInfo. Something like this:
struct PersonInfo
{
int age;
std::string name;
};
#include <iostream>
std::ostream& operator<<(std::ostream& o, const PersonInfo& p)
{
return o << p.name << " " << p.age;
}
This operator allows expressions of the type A << B, where A is an std::ostream instance (of which std::cout is one) and B is a PersonInfo instance.
This allows you do do something like this:
#include <iostream>
#include <fstream>
int main()
{
PersonInfo p = ....;
std::cout << p << std::endl; // prints name and age to stdout
// std::ofstream is also an std::ostream,
// so we can write PersonInfos to a file
std::ofstream person_file("persons.txt");
person_file << p << std::endl;
}
which in turn allows you to print the de-referenced iterator.
The result of *it is an L-value of type PersonInfo. The compiler is complaining that there is no operator<< which takes a right-hand side argument of type PersonInfo.
For the code to work, you need to provide such an operator, for example like this:
std::ostream& operator<< (std::ostream &str, const PersonInfo &p)
{
str << "Name: " << p.name << "\nAge: " << p.age << '\n';
return str;
}
The exact implementation of the operator depends on your needs for representing the class in output, of course.
What it's telling you is that there isn't a known wway to cout (console output) the contents of *it.
it is an iterator - think of this like a pointer in a list
the list is info so *it is current item in the info, which is a list of PersonInfo items.
So cout << *it; says output to the console the PersonInfo that it is currently referencing.
But the error message is telling you that the compiler doens't know how PersonInfo should be rendered to the console.
What you need to do is create an operator called << that takes an object that cout is (ostream) and a PersonInfo object and then writes the various bits of the PersonInfo to cout.
When you're overloading the << operator for a class (pretend this is defined as a friend in SomeClass), why do you both take a reference to the ostream and return that ostream?
ostream& operator<<(ostream& s, const SomeClass& c) {
//whatever
return s;
}
What benefit can returning the ostream be when it was already directly modifiable by reference? This seems redundant to me - though I'm sure it's not :)
It allows to "chain" output together. As in :
std::cout << someObj << someValue;
This is equivalent to something like :
operator<<(operator<<(std::cout, someObj), someValue);
This is not redundant, but useful for chaining calls. It's easier to see with functions like std::string::append, so I'll start with that:
std::string mystring("first");
mystring.append(" second");
mystring.append(" third");
can be rewritten as:
std::string mystring("first").append(" second").append(" third");
This is possible because .append() returns a reference to the string it modified, so we can keep adding .append(...) to the end. The code correlating to what you are doing is changing from this:
std::cout << "first";
std::cout << " second";
std::cout << " third";
into this. Since operator<< returns the stream, we can also chain these!
std::cout << "first" << " second" << " third";
see the similarity, and usefulness?
So that you can write chained-invocation of operator<< as:
stream << s1 << s2 << s3 ;
If you don't return ostream&, then you cannot write it more than once.
You can think of that either as:
operator<<(operator<<(operator<<(stream, s1), s2), s3);
Or as,
((stream << s1) << s2) << s3 ;
First (stream << s1) returns stream& on which you again invoke << and it becomes stream << s2 which returns stream& on which you again invoke << and it becomes stream << s3.
Ah, this is so linked output, like
cout << "this " << "is " << "a pen" << endl;
will still work.