我正在尝试构建的某些第三方库的模板化函数中出现错误。
MSVC指向该功能并告诉我,我正在为特定呼叫做错事。 我怎么知道错误发生在哪个电话?
如果重要,这就是功能:
template <typename T> std::string ToString(T number) { std::ostringstream ss; ss << std::setprecision(NUM_TO_STRING_PRECISION); ss << number; return ss.str(); }错误是:
错误C2088'<<':类非法
I am getting some error in a templated function of some 3rd party library that I am trying to build.
MSVC points to the function and told me that I am doing something wrong for a specific call. How can I know at which call exactly the error happens?
If it matters, this is the function:
template <typename T> std::string ToString(T number) { std::ostringstream ss; ss << std::setprecision(NUM_TO_STRING_PRECISION); ss << number; return ss.str(); }The error is:
Error C2088 '<<': illegal for class
最满意答案
我怎么知道错误发生在哪个电话?
对于visual-studio来说并不是真的,但让我们看一个简单的例子 :
#include <vector> struct Foo { Foo() = delete; }; int main() { std::vector<Foo> vfoo(15); (void)vfoo; }GCC g ++输出以下错误消息:
In file included from /usr/local/include/c++/7.1.0/vector:63:0, from main.cpp:1: /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h: In instantiation of 'static _ForwardIterator std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]': /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:583:20: required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:645:44: required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Foo*; _Size = long unsigned int; _Tp = Foo]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:1347:36: required from 'void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:285:30: required from 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Foo>]' main.cpp:9:29: required from here /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:548:37: error: use of deleted function 'Foo::Foo()' return std::fill_n(__first, __n, _ValueType()); ^~~~~~~~~~~~ main.cpp:4:5: note: declared here Foo() = delete; ^~~IIRC在视觉工作室非常相似。 只需打开原始输出选项卡,然后转到最后一个note ,该note可能包含错误的真正来源 。
使用rextester进行 MSVC结果。 同样, source_file.cpp最后一个注释指向了错误的真正来源。
How can I know at which call exactly the error happens?
Not really with visual-studio, but let's look at a simple example:
#include <vector> struct Foo { Foo() = delete; }; int main() { std::vector<Foo> vfoo(15); (void)vfoo; }GCC g++ outputs these error messages:
In file included from /usr/local/include/c++/7.1.0/vector:63:0, from main.cpp:1: /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h: In instantiation of 'static _ForwardIterator std::__uninitialized_default_n_1<true>::__uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]': /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:583:20: required from '_ForwardIterator std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Foo*; _Size = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:645:44: required from '_ForwardIterator std::__uninitialized_default_n_a(_ForwardIterator, _Size, std::allocator<_Tp>&) [with _ForwardIterator = Foo*; _Size = long unsigned int; _Tp = Foo]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:1347:36: required from 'void std::vector<_Tp, _Alloc>::_M_default_initialize(std::vector<_Tp, _Alloc>::size_type) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]' /usr/local/include/c++/7.1.0/bits/stl_vector.h:285:30: required from 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const allocator_type&) [with _Tp = Foo; _Alloc = std::allocator<Foo>; std::vector<_Tp, _Alloc>::size_type = long unsigned int; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<Foo>]' main.cpp:9:29: required from here /usr/local/include/c++/7.1.0/bits/stl_uninitialized.h:548:37: error: use of deleted function 'Foo::Foo()' return std::fill_n(__first, __n, _ValueType()); ^~~~~~~~~~~~ main.cpp:4:5: note: declared here Foo() = delete; ^~~IIRC it's very similar in visual studio. Just open the raw output tab, and go to the very last note, which probably contains the real source of the error.
MSVC results with rextester. Again the very last notes for source_file.cpp point to the real source of the error.
更多推荐
发布评论