I am writing a small program in C++. There is a class. Can you use cout in the class to output data. Such as cout << "Good times";
Don't you need then to overload << operator?
I am trying to do that but it does not work only some errors. How to output data via cout having it in the class.
CPP file:
#include "University.hpp"
#include "Course.hpp"
//#include "Lecture.hpp"
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
University::University() {
}
University::University(int capacity, string uni) {
university = uni;
cap = capacity;
length = 0;
myCourse = new Course[cap];
}
University::University(University& orig) {
copy(orig);
}
University::~University() {
delete [] myCourse;
}
University & University:: operator=(University & other){
if(this != &other){
delete [] myCourse;
copy(other);
}
return *this;
}
void University:: copy(University & other){
if(this != &other){
length = other.length;
cap = other.cap;
myCourse = new Course[cap];
for(int x = 0; x < length; x++){
myCourse[x] = other.myCourse[x];
}
}
}
void University:: addCourse(Course syllabus){
if(length == cap){
cap *= 2;
Course * temp = new Course[cap];
for(int x = 0; x < length; x++){
temp[x] = myCourse[x];
}
myCourse = temp;
}
myCourse[length] = syllabus;
length++;
}
void University::listAll(){
for(int x = 0; x < length; x++){
cout << toString();// toString();
}
}
friend ostream& operator<<(ostream& os, const University& uni){
os <<uni.university <<uni.myCourse;
return os;
}
string University::toString(){
ostringstream ans;
for(int x = 0; x < length; x++){
ans << "The University " << university << " teaches " << myCourse[x] << myCourse[x].toString() << endl;
}
return ans.str();
}
Header:
#include <string>
#include "Course.hpp"
using namespace std;
#ifndef UNIVERSITY_HPP
#define UNIVERSITY_HPP
class University {
friend ostream& operator<< (ostream& os, const University& uni);
public:
University();
University(int capacity, string UoL);
University(University& orig);
~University();
void copy(University & other);
University & operator=(University & other);
void addCourse(Course syllabus);
void listAll();
string toString();
private:
Course * myCourse;
int length;
int cap;
string university;
};
#endif /* UNIVERSITY_HPP */
errors:
g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/University.o.d -o build/Debug/Cygwin-Windows/University.o University.cpp
University.cpp:72: error: can't initialize friend function `operator<<'
University.cpp:72: error: friend declaration not in class definition
University.cpp: In member function `std::string University::toString()':
University.cpp:81: error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)((std::basic_ostream<char, std::char_traits<char> >*)(&ans))), ((const char*)"The University ")))), ((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(((std::string*)((University*)this)) + 12u)))))), ((const char*)" teaches ")) << *(((University*)this)->University::myCourse + (+(((unsigned int)x) * 16u)))'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:63: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:74: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>&(*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:86: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base&(*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:155: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:98: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:178: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:189: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:193: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:179: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:214: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:238: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:219: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:261: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:284: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:307: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
University.cpp:72: note: std::ostream& operator<<(std::ostream&, const University&)
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:504: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:499: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:612: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:567: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:465: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:460: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/ostream.tcc:505: note: std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char) [with _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/ostream:449: note: std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char) [with _CharT = char, _Traits = std::char_traits<char>]
make[2]: *** [build/Debug/Cygwin-Windows/University.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2'
make[1]: Leaving directory `/cygdrive/g/Aristotelis/C++/Assessment_2'
BUILD FAILED (exit value 2, total time: 3s)
You're trying to call ostringstream::operator<< with the argument being a Course. You need to define operator<< for Course as well. Sorry I missed this before. You should also remove the friend from the definition of operator<<(ostream&, const University&) though since it's only needed inside the class definition.
Try this example.
#include <iostream>
using namespace std;
class A
{
friend ostream& operator<<(ostream& os, const A& a);
int _member;
public:
A() : _member(0)
{ }
A(int member) : _member(member)
{ }
};
ostream& operator<<(ostream&os, const A& a)
{
os << a._member;
return os;
}
int main()
{
A a(5);
cout << a << endl;
return 0;
}
Play with it and fit for your needs.
University.cpp:72: error: can't initialize friend function `operator<<'
University.cpp:72: error: friend declaration not in class definition
University.cpp: In member function `std::string University::toString()':
So, we look In member function `std::string University::toString()' and find:
ans << ... << myCourse[x] << ...;
Are you sure you have the overload for this?
friend ostream& operator<< (ostream& os, const Course& uni);
It's not in any of the code you show here.
Related
Here is my code, I'm not sure why the error is being thrown - the method is supposed to operate on a pointer object and print out its values.
main:
cout<<"Deleted item is: "<<displayRecord(tmp)/*tmp->entry*/<<endl;
void displayRecord(PRecord* pr) {
cout<<"Time: "<<pr->time<<"\tEntry data: \""<<pr->entry<<'"'<<endl;
}
header.h:
#include <iostream>
using namespace std;
struct PRecord {
long time;
string entry;
struct PRecord *link;
};
void displayRecord(PRecord* pr);
I am getting this error: error: no match for 'operator <<
error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Deleted item is: ")) << Priority_Queue::displayRecord(tmp)'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux
The error is clear you want to use
cout << "Deleted item is: " << Priority_Queue::displayRecord(tmp);
but it doesn't compile because displayRecord returns no value. It returns void type what is not printable and cannot be used as argument for operator<<.
Call
cout << "Deleted item is: ";
Priority_Queue::displayRecord(tmp);
I have a test where I need to print out std::pair<std::string, std::string> but even though that I declare and define an operator for that googletest will comlain that it cannot find it.
Having google test manual mention that
// It's important that the << operator is defined in the SAME
// namespace that defines Bar. C++'s look-up rules rely on that.
Should I reopen std namespace and place my operator<<(std::ostream& os, const std::pair<std::string, std::string>& p) there?
Excerpt from code:
// included from somewhere else in legacy code:
// typedef ::std::map< ::std::string, ::std::string > AttrList;
bool UserDefinedTypeMatcher::MatchAndExplain(UserDefinedType& r, testing::MatchResultListener* listener) const {
typedef AttrList::const_iterator AttrIt;
std::pair<AttrIt, AttrIt> pattr = std::mismatch(r.attr.begin(), r.attr.end(), expectedUserDefinedType.attr.begin());
if(pattr.first != r.attr.end() && pattr.second != expectedUserDefinedType.attr.end())
{
*listener << "\nFail: attr members differ on the following elements: "
<< "from given UserDefinedType: " << *pattr.first << '\n'
<< ", from expected UserDefinedType : " << *pattr.second << '\n';
return false;
}
return true;
}
Compiler error:
/opt/gmock-1.7.0/include/gmock/gmock-matchers.h: In member function ‘testing::MatchResultListener& testing::MatchResultListener::operator<<(const T&) [with T = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]’:
test/src/matchers/UserDefinedTypeMatchers.cpp:50: instantiated from here
/opt/gmock-1.7.0/include/gmock/gmock-matchers.h:93: error: no match for ‘operator<<’ in ‘*(std::ostream*)((testing::MatchResultListener*)this)->testing::MatchResultListener::stream_ << x’
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
/opt/gmock-1.7.0/gtest/include/gtest/gtest-message.h:232: note: std::ostream& testing::operator<<(std::ostream&, const testing::Message&)
/opt/gmock-1.7.0/gtest/include/gtest/gtest-test-part.h:120: note: std::ostream& testing::operator<<(std::ostream&, const testing::TestPartResult&)
EDIT:
Ok, so I've learned that I can do the following:
bool UserDefinedTypeMatcher::MatchAndExplain(UserDefinedType& r, testing::MatchResultListener* listener) const {
std::pair<std::string, std::string> mypair;
// use and manipulate mypair
*listener << ::testing::PrintToString(mypair);
}
I am facing following error and I dont understand if there is a friend available for the operator << on argsMap why is it still cribbing?
Can someone please help
Args.C: In function âstd::ostream& operator<<(std::ostream&, const Args&)â:
Args.C:166: error: no match for âoperator<<â in âos << ((const Args*)a)->Args::getArgL()â
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:117: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:127: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:165: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:91: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:180: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:105: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:191: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:200: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:209: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:221: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ostream.tcc:119: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
Args.C:165: note: std::ostream& operator<<(std::ostream&, const Args&)
../../../../src/hp/lib/libcfa/Args.h:79: note: std::ostream& operator<<(std::ostream&, const ArgItem&)
Below is the code split in .h and .cpp files
typedef map<string,ArgItem,std::less<string>, std::allocator<string> > argsMap;
class Args {
const argsMap &getArgL(void) const { return lArgItems ; }
friend ostream& operator<< ( ostream &os, const argsMap& am );
friend ostream& operator<< ( ostream &os, const Args& a );
};
ostream& operator<< ( std::ostream &os, const Args& a ){
os<<a.getArgL();
return os;
}
ostream& operator<< ( std::ostream &os, const argsMap& am ){
for ( argIter2 i=am.begin() ; i != am.end() ; i++){
os<<"-"<< (*i).first <<" " << (*i).second <<"\n";
}
return os;
}
Try putting a forward declaration of ostream& operator<< ( std::ostream &os, const argsMap& am ) above ostream& operator<< ( std::ostream &os, const Args& a )
ostream& operator<< ( std::ostream &os, const argsMap& am );
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ THIS
ostream& operator<< ( std::ostream &os, const Args& a ){
os<<a.getArgL();
return os;
}
ostream& operator<< ( std::ostream &os, const argsMap& am ){
for ( argIter2 i=am.begin() ; i != am.end() ; i++){
os<<"-"<< (*i).first <<" " << (*i).second <<"\n";
}
return os;
}
The following raises a compile-time error. I'm not sure how to call one objects
overloaded operator << within another objects << operator? If anyone could point out what I'm doing wrong, that'd be really helpful .Thanks for your time!
card.cpp:
#include "card.h"
#include <iostream>
ostream& operator<<(ostream &out, Card &cCard)
{
out << cCard.getValueString()
<< " OF "
<< cCard.getSuitString()
<< "S\n";
return out;
}
string Card::getValueString()
{
Value value = getValue();
switch(value)
{
case TWO: return "TWO";
case THREE: return "THREE";
case FOUR: return "FOUR";
case FIVE: return "FIVE";
case SIX: return "SIX";
case SEVEN: return "SEVEN";
case EIGHT: return "EIGHT";
case NINE: return "NINE";
case TEN: return "TEN";
case JACK: return "JACK";
case QUEEN: return "QUEEN";
case KING: return "KING";
case ACE: return "ACE";
case NONE_VALUE: return "NONE_VALUE";
default: return "UNKNOWN VALUE";
};
}
string Card::getSuitString()
{
Suit suit = getSuit();
switch(suit)
{
case SPADE: return "SPADE";
case HEART: return "HEART";
case CLUB: return "CLUB";
case DIAMOND: return "DIAMOND";
case NONE_SUIT: return "NONE_SUIT";
default: return "UNKNOWN SUIT";
};
}
card.h
#pragma once
#include <iostream>
using namespace std;
enum Suit {
SPADE,
HEART,
CLUB,
DIAMOND,
NONE_SUIT
};
enum Value {
TWO=2,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE,
NONE_VALUE
};
class Card
{
public:
Card() : m_suit(NONE_SUIT), m_value(NONE_VALUE) { };
Card(Suit suit, Value value) : m_suit(suit), m_value(value) { };
Value getValue() { return m_value; };
Suit getSuit() { return m_suit; };
void setValue(Value value) { m_value = value; };
void setSuit(Suit suit) { m_suit = suit; };
string getValueString();
string getSuitString();
friend ostream& operator<<(ostream &out, Card &cCard);
private:
Suit m_suit;
Value m_value;
};
hand.cpp:
#include "hand.h"
#include "card.h"
ostream& operator<<(ostream &out, Card &cCard);
Hand::Hand()
{
}
Card Hand::getCard(int cardIndex)
{
if(cardIndex == 0 || cardIndex == 1)
{
return cards[cardIndex];
}
else
{
cout << "Card index out of bounds.\n";
}
return Card(NONE_SUIT, NONE_VALUE);
}
void Hand::setCard(int cardIndex, Suit suit, Value value)
{
if(cardIndex == 0 || cardIndex == 1)
{
cards[cardIndex].setValue(value);
cards[cardIndex].setSuit(suit);
}
else
{
cout << "Card index out of bounds.\n";
}
};
ostream& operator<<(ostream &out, Hand &cHand)
{
out << cHand.getCard(0)
<< "\n"
<< cHand.getCard(1);
return out;
}
hand.h:
#pragma once
#include <iostream>
#include "card.h"
using namespace std;
class Hand
{
public:
Hand();
void setCard(int cardIndex, Suit suit, Value value);
Card getCard(int cardIndex);
friend ostream& operator<<(ostream &out, Hand &cHand);
private:
Card cards[2];
};
The error is the following:
hand.cpp: In function ‘std::ostream& operator<<(std::ostream&, Hand&)’:
hand.cpp:40: error: no match for ‘operator<<’ in ‘out << ((Hand*)cHand)->Hand::getCard(0)’
/usr/include/c++/4.2.1/ostream:112: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:121: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:131: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:169: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:173: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:177: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/bits/ostream.tcc:92: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:184: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/bits/ostream.tcc:106: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:195: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:204: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:208: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:213: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:217: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:225: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/ostream:229: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits]
/usr/include/c++/4.2.1/bits/ostream.tcc:120: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits]
hand.cpp:4: note: std::ostream& operator<<(std::ostream&, Card&)
hand.cpp:38: note: std::ostream& operator<<(std::ostream&, Hand&)
Any ideas on what Im missing?
Your operator << functions should take the right hand side argument by const reference. It's telling you that it can't bind the temporary returned by getCard to the non-const reference argument of the operator.
The incredibly long error message is because there are so incredibly many overloads of << in scope at any one time, and the compiler is listing all of them as, "I tried this one but it didn't work."
Here's a small test program I wrote:
#include <iostream>
using namespace std;
class A {
public:
int val;
A(int _val=0):val(_val) { }
A operator+(A &a) { return A(val + a.val); }
A operator-(A &a) { return A(val - a.val); }
friend ostream& operator<<(ostream &, A &);
};
ostream& operator<<(ostream &out, A &a) {
out<<a.val;
return out;
}
int main() {
A a(3), b(4), c = b - a;
cout<<c<<endl; // this works
cout<<(b-a)<<endl; // this doesn't
return 0;
}
I can't seem to get why the line marked "this works" works and the one marked "this doesn't" doesn't. When I try to compile the program with the cout<<(b-a); line, here's what I get:
[felix#the-machine C]$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:26:13: error: no match for ‘operator<<’ in ‘std::cout << b.A::operator-(((A&)(& a)))’
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:108:7: note: candidates are: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:127:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:165:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:169:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:173:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/bits/ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:180:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/bits/ostream.tcc:105:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:191:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:200:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:204:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:209:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:213:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:221:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/ostream:225:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-pc-linux-gnu/4.5.0/../../../../include/c++/4.5.0/bits/ostream.tcc:119:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
test.cpp:18:11: note: std::ostream& operator<<(std::ostream&, A&)
[felix#the-machine C]$
Quite nasty.
Your operator+ returns a temporary object - and in C++ you can't bind temporaries to non-const references. You want:
ostream& operator<<(ostream &out, const A &a) {
This is the canonical way to write opertaor<< for streaming - the thing being output should always be passed as a const reference.
Because you are not allowing temporaries to be passed to your insertion operator. Change it to:
friend ostream& operator<<(ostream &, const A &);
...
ostream& operator<<(ostream &out, const A &a) {
out<<a.val;
return out;
}
in the second line,you're asking the compiler to convert from A to A&, but A is a temporary, and that is non-standard behaviour. In the first line c is not temporary so it works.
Changing your operator to
ostream& operator<<(ostream &out, const A &a)
will not only be more correct, but should also be accepted by the compiler.
A temporary object is basically considered const, so for a reference to bind to it, it has to be a reference to const:
ostream& operator<<(ostream &out, A const &a) // ...
You're passing a temporary object to operator<<, so it needs to take a const A&.
std::ostream& operator<<(std::ostream&, const A&)
A few other notes:
Always use 'const T&' when passing non-primitive arguments that are not modified. This allows you to pass temporaries, and indicates that you don't change the parameter.
It's best to mark single argument constructors explicit, this prevents you from making accidental temporary objects everywhere, killing your performance.
explicit A(int _val=0):val(_val) { }
The std::basic_ostream::operator<< expects const-qualified argument in your use case. Thus, you need to correct the signature of your operator as follows:
ostream& operator<<(ostream &out, A const& a)
By the way, the friendship is redundant because A::val is public, thus accessible anyway.