c ++程序上的奇数编译错误

问题描述 投票:0回答:1

因此,我正在尝试制作一个程序,以读取用户的输入,并将其转换为这样的注释:

--- time
note

这里是代码:

#include <iostream>                                                              
#include <fstream>                                                               
#include <ctime>                                                                 
#include <cstdlib>                                                               
using namespace std;                                                             
int main () {                                                                    
    string notesvar{"/notes"};                                                   
    string home{std::getenv("HOME")};                                            
    string notes{home, notesvar};                                                
    time_t now = time(0);                                                        
    char* dt = ctime(&now);                                                      

    ofstream myfile;                                                             
    std::cout << ": ";                                                           
    string x{};                                                                  
    std::cin >> x;                                                               
    myfile.open ("placeholder", ios_base::app);                                  
    myfile << "---- " << dt;                                                     
    myfile << x << '\n';                                                         
    myfile << "\n";                                                              
    myfile.close();                                                              
    return 0;                                                                    
} 

这会编译很多注释,但是这里是编译日志:

notes.cpp: In function ‘int main()’:
notes.cpp:9:32: error: no matching function for call to ‘std::__cxx11::basic_string<char>::basic_string(<brace-enclosed initializer list>)’
    9 |     string notes{home, notesvar};
      |                                ^
In file included from /usr/include/c++/9.3.0/string:55,
                 from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
                 from /usr/include/c++/9.3.0/bits/ios_base.h:41,
                 from /usr/include/c++/9.3.0/ios:42,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:650:2: note: candidate: ‘template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, const _Alloc&)’
  650 |  basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
      |  ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:650:2: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/9.3.0/bits/move.h:55,
                 from /usr/include/c++/9.3.0/bits/nested_exception.h:40,
                 from /usr/include/c++/9.3.0/exception:144,
                 from /usr/include/c++/9.3.0/ios:39,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/type_traits: In substitution of ‘template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = std::integral_constant<bool, false>::value; _Tp = void]’:
/usr/include/c++/9.3.0/bits/basic_string.h:117:8:   required by substitution of ‘template<class _CharT, class _Traits, class _Alloc> template<class _Tp, class _Res> using _If_sv = std::enable_if_t<std::__and_<std::is_convertible<const _Tp&, std::basic_string_view<_CharT, _Traits> >, std::__not_<std::is_convertible<const _Tp*, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>*> >, std::__not_<std::is_convertible<const _Tp&, const _CharT*> > >::value, _Res> [with _Tp = std::__cxx11::basic_string<char>; _Res = void; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
/usr/include/c++/9.3.0/bits/basic_string.h:648:30:   required from here
/usr/include/c++/9.3.0/type_traits:2384:11: error: no type named ‘type’ in ‘struct std::enable_if<false, void>’
 2384 |     using enable_if_t = typename enable_if<_Cond, _Tp>::type;
      |           ^~~~~~~~~~~
In file included from /usr/include/c++/9.3.0/string:55,
                 from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
                 from /usr/include/c++/9.3.0/bits/ios_base.h:41,
                 from /usr/include/c++/9.3.0/ios:42,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:639:2: note: candidate: ‘template<class _Tp, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Tp&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&)’
  639 |  basic_string(const _Tp& __t, size_type __pos, size_type __n,
      |  ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:639:2: note:   template argument deduction/substitution failed:
notes.cpp:9:32: note:   candidate expects 4 arguments, 2 provided
    9 |     string notes{home, notesvar};
      |                                ^
In file included from /usr/include/c++/9.3.0/string:55,
                 from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
                 from /usr/include/c++/9.3.0/bits/ios_base.h:41,
                 from /usr/include/c++/9.3.0/ios:42,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:625:9: note: candidate: ‘template<class _InputIterator, class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&)’
  625 |         basic_string(_InputIterator __beg, _InputIterator __end,
      |         ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:625:9: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/9.3.0/bits/stl_algobase.h:65,
                 from /usr/include/c++/9.3.0/bits/char_traits.h:39,
                 from /usr/include/c++/9.3.0/ios:40,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/bits/stl_iterator_base_types.h: In substitution of ‘template<class _InIter> using _RequireInputIter = typename std::enable_if<std::is_convertible<typename std::iterator_traits<_Iterator>::iterator_category, std::input_iterator_tag>::value>::type [with _InIter = std::__cxx11::basic_string<char>]’:
/usr/include/c++/9.3.0/bits/basic_string.h:621:9:   required from here
/usr/include/c++/9.3.0/bits/stl_iterator_base_types.h:232:11: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<std::__cxx11::basic_string<char> >’
  232 |     using _RequireInputIter = typename
      |           ^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9.3.0/string:55,
                 from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
                 from /usr/include/c++/9.3.0/bits/ios_base.h:41,
                 from /usr/include/c++/9.3.0/ios:42,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:587:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  587 |       basic_string(basic_string&& __str, const _Alloc& __a)
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:587:56: note:   no known conversion for argument 2 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘const std::allocator<char>&’
  587 |       basic_string(basic_string&& __str, const _Alloc& __a)
      |                                          ~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:583:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  583 |       basic_string(const basic_string& __str, const _Alloc& __a)
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:583:61: note:   no known conversion for argument 2 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘const std::allocator<char>&’
  583 |       basic_string(const basic_string& __str, const _Alloc& __a)
      |                                               ~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:579:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::initializer_list<_Tp>, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  579 |       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:579:45: note:   no known conversion for argument 1 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘std::initializer_list<char>’
  579 |       basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:552:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  552 |       basic_string(basic_string&& __str) noexcept
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:552:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:540:7: note: candidate: ‘template<class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, _CharT, const _Alloc&)’
  540 |       basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:540:7: note:   template argument deduction/substitution failed:
notes.cpp:9:18: note:   cannot convert ‘home’ (type ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’}) to type ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’}
    9 |     string notes{home, notesvar};
      |                  ^~~~
In file included from /usr/include/c++/9.3.0/string:55,
                 from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
                 from /usr/include/c++/9.3.0/bits/ios_base.h:41,
                 from /usr/include/c++/9.3.0/ios:42,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:525:7: note: candidate: ‘template<class> std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&)’
  525 |       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:525:7: note:   template argument deduction/substitution failed:
notes.cpp:9:18: note:   cannot convert ‘home’ (type ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’}) to type ‘const char*’
    9 |     string notes{home, notesvar};
      |                  ^~~~
In file included from /usr/include/c++/9.3.0/string:55,
                 from /usr/include/c++/9.3.0/bits/locale_classes.h:40,
                 from /usr/include/c++/9.3.0/bits/ios_base.h:41,
                 from /usr/include/c++/9.3.0/ios:42,
                 from /usr/include/c++/9.3.0/ostream:38,
                 from /usr/include/c++/9.3.0/iostream:39,
                 from notes.cpp:1:
/usr/include/c++/9.3.0/bits/basic_string.h:510:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
  510 |       basic_string(const _CharT* __s, size_type __n,
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:510:34: note:   no known conversion for argument 1 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘const char*’
  510 |       basic_string(const _CharT* __s, size_type __n,
      |                    ~~~~~~~~~~~~~~^~~
/usr/include/c++/9.3.0/bits/basic_string.h:492:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
  492 |       basic_string(const basic_string& __str, size_type __pos,
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:492:7: note:   candidate expects 4 arguments, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:476:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
  476 |       basic_string(const basic_string& __str, size_type __pos,
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:476:7: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:461:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]’
  461 |       basic_string(const basic_string& __str, size_type __pos,
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:461:57: note:   no known conversion for argument 2 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘std::__cxx11::basic_string<char>::size_type’ {aka ‘long unsigned int’}
  461 |       basic_string(const basic_string& __str, size_type __pos,
      |                                               ~~~~~~~~~~^~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:448:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  448 |       basic_string(const basic_string& __str)
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:448:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:440:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  440 |       basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:440:7: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:431:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  431 |       basic_string()
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:431:7: note:   candidate expects 0 arguments, 2 provided
/usr/include/c++/9.3.0/bits/basic_string.h:145:7: note: candidate: ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::__sv_wrapper, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
  145 |       basic_string(__sv_wrapper __svw, const _Alloc& __a)
      |       ^~~~~~~~~~~~
/usr/include/c++/9.3.0/bits/basic_string.h:145:33: note:   no known conversion for argument 1 from ‘std::string’ {aka ‘std::__cxx11::basic_string<char>’} to ‘std::__cxx11::basic_string<char>::__svT_wrapper’
  145 |       basic_string(__sv_wrapper __svw, const _Alloc& __a)
      |     

它是使用g++ -std=c++17 -o notes notes.cpp编译的。抱歉,这是一个愚蠢的问题。我只是从C ++开始。令我感到困惑的是……232行上的错误?!预先感谢您的帮助!

c++
1个回答
0
投票
string notes{home, notesvar};

没有std::string(std::string, std::string)构造函数。要将另一个字符串构造为其他两个字符串的串联,只需operator +将其添加。

string notes = home + notesvar;
© www.soinside.com 2019 - 2024. All rights reserved.