前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >蓝桥ROS机器人之现代C++学习笔记第2章习题

蓝桥ROS机器人之现代C++学习笔记第2章习题

作者头像
zhangrelay
发布2022-04-29 19:38:19
3080
发布2022-04-29 19:38:19
举报

学完后,看了下习题,都不会做,还好作者给出的答案。

为何学习现代C++,主要我读本科系统学习C++是20年前,需要补充一下知识。

讲个“惊悚的故事”,我本科所学的C++,现在高校还是那么讲的,教材几乎没啥大变化。

没办法,我就是上课小老师,课程培养方案这种,我是无能为力的,只能做好自己份内的工作。

作者总结很简洁:

有一些是C++17/20的新功能,学完后感觉眼花缭乱。 

第一题: 

代码语言:javascript
复制
#include <iostream>
#include <map>
#include <string>
#include <functional>

template <typename Key, typename Value, typename F>
void update(std::map<Key, Value>& m, F foo) {
    for (auto&& [key, value] : m ) value = foo(key);
}

int main() {
    std::map<std::string, long long int> m {
        {"a", 1},
        {"b", 2},
        {"c", 3}
    };
    update(m, [](std::string key) -> long long int {
        return std::hash<std::string>{}(key);
    });
    for (auto&& [key, value] : m)
        std::cout << key << ":" << value << std::endl;
}

g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out

第二题:

代码语言:javascript
复制
#include <iostream>
template<typename ... T>
auto average(T ... t) {
    return (t + ... ) / sizeof...(t);
}
int main() {
    std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
}

g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp && ./a.out 


蓝桥出错记录:

代码语言:javascript
复制
shiyanlou:Code/ $ git clone https://github.com/changkun/modern-cpp-tutorial.git
\u6b63\u514b\u9686\u5230 'modern-cpp-tutorial'...
^C
shiyanlou:Code/ $ git clone https://github.com/changkun/modern-cpp-tutorial.git
\u6b63\u514b\u9686\u5230 'modern-cpp-tutorial'...




fatal: unable to access 'https://github.com/changkun/modern-cpp-tutorial.git/': Failed to connect to github.com port 443: \u8fde\u63a5\u8d85\u65f6
shiyanlou:Code/ $                                                    [22:34:28]
shiyanlou:Code/ $                                                    [22:34:28]
shiyanlou:Code/ $                                                    [22:34:28]
shiyanlou:Code/ $                                                    [22:34:28]
shiyanlou:Code/ $ gedit ex1.cpp                                      [22:34:28]

(gedit:266): GVFS-RemoteVolumeMonitor-WARNING **: remote volume monitor with dbus name org.gtk.vfs.UDisks2VolumeMonitor is not supported

(gedit:266): Gtk-WARNING **: Calling Inhibit failed: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files
shiyanlou:Code/ $ g++ -std=c++17 ex1.cpp -o ex1                      [22:36:44]
ex1.cpp: In function \u2018void update(std::map<Key, Value>&, F)\u2019:
ex1.cpp:8:17: error: expected unqualified-id before \u2018[\u2019 token
     for (auto&& [key, value] : m ) value = foo(key);
                 ^
ex1.cpp:8:17: error: expected \u2018;\u2019 before \u2018[\u2019 token
ex1.cpp:8:18: error: \u2018key\u2019 was not declared in this scope
     for (auto&& [key, value] : m ) value = foo(key);
                  ^
ex1.cpp:8:23: error: \u2018value\u2019 was not declared in this scope
     for (auto&& [key, value] : m ) value = foo(key);
                       ^
ex1.cpp: In lambda function:
ex1.cpp:8:30: error: expected \u2018{\u2019 before \u2018:\u2019 token
     for (auto&& [key, value] : m ) value = foo(key);
                              ^
ex1.cpp: In function \u2018void update(std::map<Key, Value>&, F)\u2019:
ex1.cpp:8:30: error: expected \u2018;\u2019 before \u2018:\u2019 token
ex1.cpp:8:30: error: expected primary-expression before \u2018:\u2019 token
ex1.cpp:8:30: error: expected \u2018)\u2019 before \u2018:\u2019 token
ex1.cpp:8:30: error: expected primary-expression before \u2018:\u2019 token
ex1.cpp: In function \u2018int main()\u2019:
ex1.cpp:20:17: error: expected unqualified-id before \u2018[\u2019 token
     for (auto&& [key, value] : m)
                 ^
ex1.cpp:20:17: error: expected \u2018;\u2019 before \u2018[\u2019 token
ex1.cpp:20:18: error: \u2018key\u2019 was not declared in this scope
     for (auto&& [key, value] : m)
                  ^
ex1.cpp:20:23: error: \u2018value\u2019 was not declared in this scope
     for (auto&& [key, value] : m)
                       ^
ex1.cpp: In lambda function:
ex1.cpp:20:30: error: expected \u2018{\u2019 before \u2018:\u2019 token
     for (auto&& [key, value] : m)
                              ^
ex1.cpp: In function \u2018int main()\u2019:
ex1.cpp:20:30: error: expected \u2018;\u2019 before \u2018:\u2019 token
ex1.cpp:20:30: error: expected primary-expression before \u2018:\u2019 token
ex1.cpp:20:30: error: expected \u2018)\u2019 before \u2018:\u2019 token
ex1.cpp:20:30: error: expected primary-expression before \u2018:\u2019 token
shiyanlou:Code/ $ gedit ex2.cpp                                      [22:37:09]

(gedit:317): GVFS-RemoteVolumeMonitor-WARNING **: remote volume monitor with dbus name org.gtk.vfs.UDisks2VolumeMonitor is not supported

(gedit:317): Gtk-WARNING **: Calling Inhibit failed: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files
shiyanlou:Code/ $ g++ -std=c++17 ex2.cpp -o ex2                      [22:40:47]
ex2.cpp: In function \u2018auto average(T ...)\u2019:
ex2.cpp:4:17: error: expected primary-expression before \u2018...\u2019 token
     return (t + ... ) / sizeof...(t);
                 ^
ex2.cpp:4:17: error: expected \u2018)\u2019 before \u2018...\u2019 token
ex2.cpp:4:36: error: parameter packs not expanded with \u2018...\u2019:
     return (t + ... ) / sizeof...(t);
                                    ^
ex2.cpp:4:36: note:         \u2018t\u2019
ex2.cpp: In function \u2018int main()\u2019:
ex2.cpp:7:15: error: no match for \u2018operator<<\u2019 (operand types are \u2018std::ostream {aka std::basic_ostream<char>}\u2019 and \u2018void\u2019)
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
               ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:108:7: note: candidate: 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>]
       operator<<(__ostream_type& (*__pf)(__ostream_type&))
       ^
/usr/include/c++/5/ostream:108:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}\u2019
/usr/include/c++/5/ostream:117:7: note: candidate: 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>]
       operator<<(__ios_type& (*__pf)(__ios_type&))
       ^
/usr/include/c++/5/ostream:117:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}\u2019
/usr/include/c++/5/ostream:127:7: note: candidate: 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>]
       operator<<(ios_base& (*__pf) (ios_base&))
       ^
/usr/include/c++/5/ostream:127:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018std::ios_base& (*)(std::ios_base&)\u2019
/usr/include/c++/5/ostream:166:7: note: candidate: 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>]
       operator<<(long __n)
       ^
/usr/include/c++/5/ostream:166:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018long int\u2019
/usr/include/c++/5/ostream:170:7: note: candidate: 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>]
       operator<<(unsigned long __n)
       ^
/usr/include/c++/5/ostream:170:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018long unsigned int\u2019
/usr/include/c++/5/ostream:174:7: note: candidate: 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>]
       operator<<(bool __n)
       ^
/usr/include/c++/5/ostream:174:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018bool\u2019
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from ex2.cpp:1:
/usr/include/c++/5/bits/ostream.tcc:91:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
     basic_ostream<_CharT, _Traits>::
     ^
/usr/include/c++/5/bits/ostream.tcc:91:5: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018short int\u2019
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:181:7: note: candidate: 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>]
       operator<<(unsigned short __n)
       ^
/usr/include/c++/5/ostream:181:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018short unsigned int\u2019
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from ex2.cpp:1:
/usr/include/c++/5/bits/ostream.tcc:105:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
     basic_ostream<_CharT, _Traits>::
     ^
/usr/include/c++/5/bits/ostream.tcc:105:5: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018int\u2019
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:192:7: note: candidate: 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>]
       operator<<(unsigned int __n)
       ^
/usr/include/c++/5/ostream:192:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018unsigned int\u2019
/usr/include/c++/5/ostream:201:7: note: candidate: 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>]
       operator<<(long long __n)
       ^
/usr/include/c++/5/ostream:201:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018long long int\u2019
/usr/include/c++/5/ostream:205:7: note: candidate: 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>]
       operator<<(unsigned long long __n)
       ^
/usr/include/c++/5/ostream:205:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018long long unsigned int\u2019
/usr/include/c++/5/ostream:220:7: note: candidate: 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>]
       operator<<(double __f)
       ^
/usr/include/c++/5/ostream:220:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018double\u2019
/usr/include/c++/5/ostream:224:7: note: candidate: 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>]
       operator<<(float __f)
       ^
/usr/include/c++/5/ostream:224:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018float\u2019
/usr/include/c++/5/ostream:232:7: note: candidate: 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>]
       operator<<(long double __f)
       ^
/usr/include/c++/5/ostream:232:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018long double\u2019
/usr/include/c++/5/ostream:245:7: note: candidate: 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>]
       operator<<(const void* __p)
       ^
/usr/include/c++/5/ostream:245:7: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018const void*\u2019
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from ex2.cpp:1:
/usr/include/c++/5/bits/ostream.tcc:119:5: note: candidate: 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>]
     basic_ostream<_CharT, _Traits>::
     ^
/usr/include/c++/5/bits/ostream.tcc:119:5: note:   no known conversion for argument 1 from \u2018void\u2019 to \u2018std::basic_ostream<char>::__streambuf_type* {aka std::basic_streambuf<char>*}\u2019
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:628:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^
/usr/include/c++/5/ostream:628:5: note:   template argument deduction/substitution failed:
/usr/include/c++/5/ostream: In substitution of \u2018template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = void]\u2019:
ex2.cpp:7:55:   required from here
/usr/include/c++/5/ostream:628:5: error: forming reference to void
/usr/include/c++/5/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
     operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
     ^
/usr/include/c++/5/ostream:574:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018const unsigned char*\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
     operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
     ^
/usr/include/c++/5/ostream:569:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018const signed char*\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
     ^
/usr/include/c++/5/ostream:556:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018const char*\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from ex2.cpp:1:
/usr/include/c++/5/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
     ^
/usr/include/c++/5/bits/ostream.tcc:321:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018const char*\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
     ^
/usr/include/c++/5/ostream:539:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:55: note:   mismatched types \u2018const _CharT*\u2019 and \u2018void\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                                                       ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
     ^
/usr/include/c++/5/ostream:519:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018unsigned char\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
     ^
/usr/include/c++/5/ostream:514:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018signed char\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
     operator<<(basic_ostream<char, _Traits>& __out, char __c)
     ^
/usr/include/c++/5/ostream:508:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018char\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
     ^
/usr/include/c++/5/ostream:502:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018char\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from ex2.cpp:1:
/usr/include/c++/5/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
     ^
/usr/include/c++/5/ostream:497:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:55: note:   deduced conflicting types for parameter \u2018_CharT\u2019 (\u2018char\u2019 and \u2018void\u2019)
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                                                       ^
In file included from /usr/include/c++/5/bits/ios_base.h:46:0,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from ex2.cpp:1:
/usr/include/c++/5/system_error:209:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
     ^
/usr/include/c++/5/system_error:209:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:25: note:   cannot convert \u2018average<{int, int, int, int, int, int, int, int, int, int}>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\u2019 (type \u2018void\u2019) to type \u2018const std::error_code&\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                         ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from ex2.cpp:1:
/usr/include/c++/5/bits/basic_string.h:5172:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator<<(basic_ostream<_CharT, _Traits>& __os,
     ^
/usr/include/c++/5/bits/basic_string.h:5172:5: note:   template argument deduction/substitution failed:
ex2.cpp:7:55: note:   mismatched types \u2018const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>\u2019 and \u2018void\u2019
     std::cout << average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                                                       ^
shiyanlou:Code/ $                                                    [22:41:10]
shiyanlou:Code/ $                                                    [22:41:11]
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-04-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档