简体   繁体   中英

Constructor conversion error in this syntax

I am trying to build an example from C++ library with MSVS 2017

  #include <fstream>
  #include <vector>
  #include <algorithm>
  #include <iterator>
  #include <iostream>

  int main() {
      std::ifstream in("file.lib", std::fstream::in);

      const std::vector<char> buffer = []() {
        std::vector<char> buffer_(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
        std::replace(buffer_.begin(), buffer_.end(), '\n', ' ');
        std::replace(buffer_.begin(), buffer_.end(), '\\', ' ');
        return buffer_;
      } ();

    return 0;
}


I get several problems:

λ cmake --build .
Microsoft (R) Build Engine version 15.9.21+g9802d43bc3 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  CMake does not need to re-run because C:/Users/mulderpa/cppsandbox/sdb2/build/CMakeFiles/generate.stamp is up-to-date.
  Building Custom Rule C:/Users/mulderpa/cppsandbox/sdb2/CMakeLists.txt
  CMake does not need to re-run because C:/Users/mulderpa/cppsandbox/sdb2/build/CMakeFiles/generate.stamp is up-to-date.
  main.cc
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(18): error C2228: left of '.begin' must have class/struct/union [C:\Users\mulderpa\cppsandbox\sdb2\build\main.v cxproj]
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(18): error C2672: 'std::replace': no matching overloaded function found [C:\Users\mulderpa\cppsandbox\sdb2\buil d\main.vcxproj]
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(18): error C2780: 'void std::replace(const _FwdIt,const _FwdIt,const _Ty &,const _Ty &)': expects 4 arguments -  3 provided [C:\Users\mulderpa\cppsandbox\sdb2\build\main.vcxproj]
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\algorithm(1607): note: see declaration of 'std::repl
  ace'
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(19): error C2672: 'std::replace': no matching overloaded function found [C:\Users\mulderpa\cppsandbox\sdb2\buil d\main.vcxproj]
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(19): error C2780: 'void std::replace(const _FwdIt,const _FwdIt,const _Ty &,const _Ty &)': expects 4 arguments -  3 provided [C:\Users\mulderpa\cppsandbox\sdb2\build\main.vcxproj]
  C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Tools\MSVC\14.16.27023\include\algorithm(1607): note: see declaration of 'std::repl
  ace'
C:\Users\mulderpa\cppsandbox\sdb2\main.cc(21): error C2440: 'initializing': cannot convert from 'std::vector<char,std::allocator<char>> (__cdecl *)(std:: istreambuf_iterator<char,std::char_traits<char>>,std::istreambuf_iterator<char,std::char_traits<char>> (__cdecl *)(void))' to 'std::vector<char,std::allo cator<char>>' [C:\Users\mulderpa\cppsandbox\sdb2\build\main.vcxproj]
  C:\Users\mulderpa\cppsandbox\sdb2\main.cc(21): note: No constructor could take the source type, or constructor overload resolution was ambiguous

Not sure, I was able to build the example with g++ fine.

The comments stated the problem - buffer_ is parsed as function declaration, see Most vexing parse . Also your lambda does not capture the in stream which should definitely not compile.

Solution is to replace buffer_() with buffer_{} and capture in :

const std::vector<char> buffer = [&in]() {
        std::vector<char> buffer_{std::istreambuf_iterator<char>(in),
                                  std::istreambuf_iterator<char>()};
        std::replace(buffer_.begin(), buffer_.end(), '\n', ' ');
        std::replace(buffer_.begin(), buffer_.end(), '\\', ' ');
        return buffer_;
      } ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM