Skip to content
Snippets Groups Projects
Commit caed29fc authored by Marcus Mohr's avatar Marcus Mohr
Browse files

Updates notebooks used in class today (corrections & extensions)

parent 324b74ca
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id:e04e0b83-753f-4a58-bce3-3746e7c69650 tags: %% Cell type:markdown id:e04e0b83-753f-4a58-bce3-3746e7c69650 tags:
### Friendship ### Friendship
%% Cell type:markdown id:6c224ee8-f4c0-495c-8261-6d2c17f845a6 tags: %% Cell type:markdown id:6c224ee8-f4c0-495c-8261-6d2c17f845a6 tags:
* In many situations we tend to make the data members of our classes **private** (or protected in the case of inheritance). * In many situations we tend to make the data members of our classes **private** (or protected in the case of inheritance).
* If we do so, then no objects of other classes or free-functions have access to them. Not even other objects of the same class. * If we do so, then no objects of other classes or free-functions have access to them. Not even other objects of the same class.
* Sometimes this is too restrictive, though. We might need to provide direct access to our members for selected **friends**. * Sometimes this is too restrictive, though. We might need to provide direct access to our members for selected **friends**.
* A typical example is, when we want to overload the ```<<``` operator, to be able to place our object into an output stream. * A typical example is, when we want to overload the ```<<``` operator, to be able to place our object into an output stream.
%% Cell type:markdown id:ff1a3263-e349-4840-af2f-ad90fde352ac tags: %% Cell type:markdown id:ff1a3263-e349-4840-af2f-ad90fde352ac tags:
*** ***
Let us create a simpe demo class Let us create a simpe demo class
%% Cell type:code id:cebe299d-3bcc-4a57-af69-8aed41401244 tags: %% Cell type:code id:cebe299d-3bcc-4a57-af69-8aed41401244 tags:
``` C++11 ``` C++11
class myTuple { class myTuple {
int fir; int fir;
int sec; int sec;
public: public:
myTuple( int fir, int sec ) : fir(fir), sec(sec) {}; myTuple( int fir, int sec ) : fir(fir), sec(sec) {};
}; };
``` ```
%% Cell type:markdown id:97622c4c-2eba-47cd-8dbb-c2fe9963dbc3 tags: %% Cell type:markdown id:97622c4c-2eba-47cd-8dbb-c2fe9963dbc3 tags:
and try to test-run it and try to test-run it
%% Cell type:code id:7b80f338-bd48-4699-81aa-fbae245a3a04 tags: %% Cell type:code id:7b80f338-bd48-4699-81aa-fbae245a3a04 tags:
``` C++11 ``` C++11
#include <iostream> #include <iostream>
int main() { int main() {
myTuple obj( 3, 5 ); myTuple obj( 3, 5 );
std::cout << "Pair is: " << obj << std::endl; std::cout << "Pair is: " << obj << std::endl;
} }
main(); main();
``` ```
%% Output %% Output
input_line_9:3:28: error: invalid operands to binary expression ('basic_ostream<char, std::char_traits<char> >' and '__cling_N52::myTuple') input_line_9:3:28: error: invalid operands to binary expression ('basic_ostream<char, std::char_traits<char> >' and '__cling_N52::myTuple')
std::cout << "Pair is: " << obj << std::endl; std::cout << "Pair is: " << obj << std::endl;
~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:245:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const void *' for 1st argument; take the address of the argument with & /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:245:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const void *' for 1st argument; take the address of the argument with &
operator<<(const void* __p) operator<<(const void* __p)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/system_error:217:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const std::error_code' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/system_error:217:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const std::error_code' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e) operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:108:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::basic_ostream<char, std::char_traits<char> >::__ostream_type &(*)(std::basic_ostream<char, std::char_traits<char> >::__ostream_type &)' (aka 'basic_ostream<char, std::char_traits<char> > &(*)(basic_ostream<char, std::char_traits<char> > &)') for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:108:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::basic_ostream<char, std::char_traits<char> >::__ostream_type &(*)(std::basic_ostream<char, std::char_traits<char> >::__ostream_type &)' (aka 'basic_ostream<char, std::char_traits<char> > &(*)(basic_ostream<char, std::char_traits<char> > &)') for 1st argument
operator<<(__ostream_type& (*__pf)(__ostream_type&)) operator<<(__ostream_type& (*__pf)(__ostream_type&))
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:117:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::basic_ostream<char, std::char_traits<char> >::__ios_type &(*)(std::basic_ostream<char, std::char_traits<char> >::__ios_type &)' (aka 'basic_ios<char, std::char_traits<char> > &(*)(basic_ios<char, std::char_traits<char> > &)') for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:117:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::basic_ostream<char, std::char_traits<char> >::__ios_type &(*)(std::basic_ostream<char, std::char_traits<char> >::__ios_type &)' (aka 'basic_ios<char, std::char_traits<char> > &(*)(basic_ios<char, std::char_traits<char> > &)') for 1st argument
operator<<(__ios_type& (*__pf)(__ios_type&)) operator<<(__ios_type& (*__pf)(__ios_type&))
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:127:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::ios_base &(*)(std::ios_base &)' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:127:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::ios_base &(*)(std::ios_base &)' for 1st argument
operator<<(ios_base& (*__pf) (ios_base&)) operator<<(ios_base& (*__pf) (ios_base&))
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'long' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:166:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'long' for 1st argument
operator<<(long __n) operator<<(long __n)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:170:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned long' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:170:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned long' for 1st argument
operator<<(unsigned long __n) operator<<(unsigned long __n)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'bool' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:174:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'bool' for 1st argument
operator<<(bool __n) operator<<(bool __n)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'short' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:178:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'short' for 1st argument
operator<<(short __n); operator<<(short __n);
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned short' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:181:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned short' for 1st argument
operator<<(unsigned short __n) operator<<(unsigned short __n)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'int' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:189:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'int' for 1st argument
operator<<(int __n); operator<<(int __n);
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:192:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned int' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:192:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned int' for 1st argument
operator<<(unsigned int __n) operator<<(unsigned int __n)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'long long' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:201:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'long long' for 1st argument
operator<<(long long __n) operator<<(long long __n)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned long long' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:205:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned long long' for 1st argument
operator<<(unsigned long long __n) operator<<(unsigned long long __n)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:220:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'double' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:220:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'double' for 1st argument
operator<<(double __f) operator<<(double __f)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'float' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:224:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'float' for 1st argument
operator<<(float __f) operator<<(float __f)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'long double' for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:232:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'long double' for 1st argument
operator<<(long double __f) operator<<(long double __f)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:276:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::basic_ostream<char, std::char_traits<char> >::__streambuf_type *' (aka 'basic_streambuf<char, std::char_traits<char> > *') for 1st argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:276:7: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'std::basic_ostream<char, std::char_traits<char> >::__streambuf_type *' (aka 'basic_streambuf<char, std::char_traits<char> > *') for 1st argument
operator<<(__streambuf_type* __sb); operator<<(__streambuf_type* __sb);
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'char' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:511:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'char' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __out, char __c) operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'char' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:517:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, char __c) operator<<(basic_ostream<char, _Traits>& __out, char __c)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'signed char' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:523:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'signed char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, signed char __c) operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned char' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:528:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'unsigned char' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c) operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:565:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const char *' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:565:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, const char* __s) operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:578:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const signed char *' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:578:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const signed char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s) operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:583:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const unsigned char *' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:583:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const unsigned char *' for 2nd argument
operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s) operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/ostream.tcc:321:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const char *' for 2nd argument /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/ostream.tcc:321:5: note: candidate function not viable: no known conversion from '__cling_N52::myTuple' to 'const char *' for 2nd argument
operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s) operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:506:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. '__cling_N52::myTuple') /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:506:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. '__cling_N52::myTuple')
operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c) operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/basic_string.h:6419:5: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against '__cling_N52::myTuple' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/basic_string.h:6419:5: note: candidate template ignored: could not match 'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>' against '__cling_N52::myTuple'
operator<<(basic_ostream<_CharT, _Traits>& __os, operator<<(basic_ostream<_CharT, _Traits>& __os,
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:548:5: note: candidate template ignored: could not match 'const _CharT *' against '__cling_N52::myTuple' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:548:5: note: candidate template ignored: could not match 'const _CharT *' against '__cling_N52::myTuple'
operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s) operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:691:5: note: candidate template ignored: requirement '__and_<__not_<is_lvalue_reference<basic_ostream<char> &> >, __is_convertible_to_basic_ostream<basic_ostream<char> &>, __is_insertable<__rvalue_ostream_type<basic_ostream<char> &>, const myTuple &> >::value' was not satisfied [with _Ostream = std::basic_ostream<char> &, _Tp = __cling_N52::myTuple] /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/ostream:691:5: note: candidate template ignored: requirement '__and_<__not_<is_lvalue_reference<basic_ostream<char> &> >, __is_convertible_to_basic_ostream<basic_ostream<char> &>, __is_insertable<__rvalue_ostream_type<basic_ostream<char> &>, const myTuple &> >::value' was not satisfied [with _Ostream = std::basic_ostream<char> &, _Tp = __cling_N52::myTuple]
operator<<(_Ostream&& __os, const _Tp& __x) operator<<(_Ostream&& __os, const _Tp& __x)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/shared_ptr.h:66:5: note: candidate template ignored: could not match '__shared_ptr<type-parameter-0-2, _Lp>' against '__cling_N52::myTuple' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/shared_ptr.h:66:5: note: candidate template ignored: could not match '__shared_ptr<type-parameter-0-2, _Lp>' against '__cling_N52::myTuple'
operator<<(std::basic_ostream<_Ch, _Tr>& __os, operator<<(std::basic_ostream<_Ch, _Tr>& __os,
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream'
_DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left) _DEFINE_EXPR_BINARY_OPERATOR(<<, __shift_left)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:344:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:344:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \ operator _Op(const _Expr<_Dom1, typename _Dom1::value_type>& __v, \
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream'
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:357:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:357:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \ operator _Op(const _Expr<_Dom, typename _Dom::value_type>& __v, \
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr<type-parameter-0-0, typename type-parameter-0-0::value_type>' against '__cling_N52::myTuple' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr<type-parameter-0-0, typename type-parameter-0-0::value_type>' against '__cling_N52::myTuple'
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:370:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:370:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const typename _Dom::value_type& __t, \ operator _Op(const typename _Dom::value_type& __t, \
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr' against 'basic_ostream'
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:383:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:383:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \ operator _Op(const _Expr<_Dom,typename _Dom::value_type>& __e, \
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr<type-parameter-0-0, typename type-parameter-0-0::value_type>' against '__cling_N52::myTuple' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:413:5: note: candidate template ignored: could not match '_Expr<type-parameter-0-0, typename type-parameter-0-0::value_type>' against '__cling_N52::myTuple'
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:396:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/bits/valarray_after.h:396:5: note: expanded from macro '_DEFINE_EXPR_BINARY_OPERATOR'
operator _Op(const valarray<typename _Dom::value_type>& __v, \ operator _Op(const valarray<typename _Dom::value_type>& __v, \
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray' against 'basic_ostream' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray' against 'basic_ostream'
_DEFINE_BINARY_OPERATOR(<<, __shift_left) _DEFINE_BINARY_OPERATOR(<<, __shift_left)
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1155:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1155:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \ operator _Op(const valarray<_Tp>& __v, const valarray<_Tp>& __w) \
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray' against 'basic_ostream' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray' against 'basic_ostream'
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1166:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1166:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const valarray<_Tp>& __v, \ operator _Op(const valarray<_Tp>& __v, \
^ ^
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray<type-parameter-0-0>' against '__cling_N52::myTuple' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1193:1: note: candidate template ignored: could not match 'valarray<type-parameter-0-0>' against '__cling_N52::myTuple'
/srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1177:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR' /srv/conda/envs/notebook/bin/../lib/gcc/../../x86_64-conda-linux-gnu/include/c++/9.4.0/valarray:1177:5: note: expanded from macro '_DEFINE_BINARY_OPERATOR'
operator _Op(const typename valarray<_Tp>::value_type& __t, \ operator _Op(const typename valarray<_Tp>::value_type& __t, \
^ ^
Interpreter Error: Interpreter Error:
%% Cell type:markdown id:8b28c2a6-533e-478b-91e1-b63270121d6c tags: %% Cell type:markdown id:8b28c2a6-533e-478b-91e1-b63270121d6c tags:
Okay that obviously did not work. Problem is that ```<<``` does not know how to work with objects of type ```myTuple```. Okay that obviously did not work. Problem is that ```<<``` does not know how to work with objects of type ```myTuple```.
* We can fix this by implementing a version of ```<<``` that understands it. * We can fix this by implementing a version of ```<<``` that understands it.
* Different from our overloading of ```[ ]``` for our ```VectorClass```, we do not do this on the object. Instead we need to overload the free-function that is ```<<```. * Different from our overloading of ```[ ]``` for our ```VectorClass```, we do not do this on the object. Instead we need to overload the free-function that is ```<<```.
%% Cell type:code id:8dfa7a1c-28d8-4de1-ae19-bdb2a6d3e48e tags: %% Cell type:code id:8dfa7a1c-28d8-4de1-ae19-bdb2a6d3e48e tags:
``` C++11 ``` C++11
%%file myTuple.cpp %%file myTuple.cpp
#include <iostream> #include <iostream>
class myTuple { class myTuple {
int fir; int fir;
int sec; int sec;
public: public:
myTuple( int fir, int sec ) : fir(fir), sec(sec) {}; myTuple( int fir, int sec ) : fir(fir), sec(sec) {};
}; };
std::ostream& operator << ( std::ostream &os, const myTuple& tup ) std::ostream& operator << ( std::ostream &os, const myTuple& tup )
{] for {
os << "( " << tup.fir << " , " << tup.sec << " )" << std::endl; os << "( " << tup.fir << " , " << tup.sec << " )";
// necessary for chaining: // necessary for chaining:
return os; return os;
} }
``` ```
%% Output %% Output
Overwriting myTuple.cpp Writing myTuple.cpp
%% Cell type:code id:25e56a5e-6c64-4d77-9a81-5d808f2b63b3 tags: %% Cell type:code id:25e56a5e-6c64-4d77-9a81-5d808f2b63b3 tags:
``` C++11 ``` C++11
!g++ -Wall -Wextra -c myTuple.cpp !g++ -Wall -Wextra -c myTuple.cpp
``` ```
%% Output %% Output
myTuple.cpp: In function ‘std::ostream& operator<<(std::ostream&, const myTuple&)’: myTuple.cpp: In function ‘std::ostream& operator<<(std::ostream&, const myTuple&)’:
myTuple.cpp:15:2: error: expected primary-expression before ‘]’ token myTuple.cpp:16:23: error: ‘int myTuple::fir’ is private within this context
{] for os << "( " << tup.fir << " , " << tup.sec << " )";
^ ^~~
myTuple.cpp:14:62: warning: unused parameter ‘tup’ [-Wunused-parameter] myTuple.cpp:6:7: note: declared private here
std::ostream& operator << ( std::ostream &os, const myTuple& tup ) int fir;
^~~ ^~~
myTuple.cpp:16:43: error: ‘int myTuple::sec’ is private within this context
os << "( " << tup.fir << " , " << tup.sec << " )";
^~~
myTuple.cpp:7:7: note: declared private here
int sec;
^~~
%% Cell type:markdown id:092b284c-9b78-4ea9-9b9a-f43cc5f32612 tags: %% Cell type:markdown id:092b284c-9b78-4ea9-9b9a-f43cc5f32612 tags:
* That was to be expected. The attributes ```fir``` and ```sec``` of ```myTuple``` are private. * That was to be expected. The attributes ```fir``` and ```sec``` of ```myTuple``` are private.
* However, we do not want to make them accessible to everyone, just for the sake of one special function/operator. * However, we do not want to make them accessible to everyone, just for the sake of one special function/operator.
* But, we can do so **selectively**, with the friend statement. * But, we can do so **selectively**, with the friend statement.
%% Cell type:code id:ff341d41-7aa2-4f87-92f2-e189dd448d68 tags: %% Cell type:code id:ff341d41-7aa2-4f87-92f2-e189dd448d68 tags:
``` C++11 ``` C++11
%%file myTuple.cpp %%file myTuple.cpp
#include <iostream> #include <iostream>
class myTuple { class myTuple {
int fir; int fir;
int sec; int sec;
friend std::ostream& operator << ( std::ostream&, const myTuple& ); friend std::ostream& operator << ( std::ostream&, const myTuple& );
public: public:
myTuple( int fir, int sec ) : fir(fir), sec(sec) {}; myTuple( int fir, int sec ) : fir(fir), sec(sec) {};
}; };
std::ostream& operator << ( std::ostream &os, const myTuple& tup ) std::ostream& operator << ( std::ostream &os, const myTuple& tup )
{ {
os << "( " << tup.fir << " , " << tup.sec << " )" << std::endl; os << "( " << tup.fir << " , " << tup.sec << " )" << std::endl;
// necessary for chaining: // necessary for chaining:
return os; return os;
} }
int main() { int main() {
myTuple obj( 3, 5 ); myTuple obj( 3, 5 );
std::cout << "Pair is: " << obj << std::endl; std::cout << "Pair is: " << obj << std::endl;
} }
``` ```
%% Output %% Output
Overwriting myTuple.cpp Overwriting myTuple.cpp
%% Cell type:code id:c8560f81-10ef-4203-b3f3-8046255be25a tags: %% Cell type:code id:c8560f81-10ef-4203-b3f3-8046255be25a tags:
``` C++11 ``` C++11
!g++ -Wall -Wextra myTuple.cpp !g++ -Wall -Wextra myTuple.cpp
``` ```
%% Cell type:code id:6a5f1cb3-7891-4cb8-b8ce-d3f6aa996ecc tags: %% Cell type:code id:6a5f1cb3-7891-4cb8-b8ce-d3f6aa996ecc tags:
``` C++11 ``` C++11
!./a.out !./a.out
``` ```
%% Output %% Output
Pair is: ( 3 , 5 ) Pair is: ( 3 , 5 )
%% Cell type:code id:94e6f340-7a06-4c59-8889-5088e41712f2 tags: %% Cell type:code id:94e6f340-7a06-4c59-8889-5088e41712f2 tags:
``` C++11 ``` C++11
``` ```
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment