[英]Constructor conversion error in this syntax
我正在嘗試使用MSVS 2017從C ++庫構建一個示例
#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;
}
我遇到了幾個問題:
λ 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
不確定,我能夠用g ++構建這個例子。
注釋說明了問題 - buffer_
被解析為函數聲明,請參閱最煩惱的解析 。 你的lambda也不會捕獲肯定不會編譯的in
stream。
解決方案是更換buffer_()
與buffer_{}
和捕獲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_;
} ();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.