2017-07-03 39 views
0

我在嘗試構建某些第三方庫的模板化函數時出現了一些錯誤。從模板實例化/類型推演中查找錯誤消息的實際來源

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 '< <':類非法

+0

這是完整的錯誤嗎? –

+0

@RetiredNinja Yup –

+0

文檔說錯誤只發生在C代碼中。 https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/compiler-error-c2088它使用了多少個地方?你可能會開始評論它們,直到找到導致它的那個。 –

回答

1

我怎麼知道在w究竟是否發生錯誤?

不是真的有,但讓我們來看一個簡單的example

#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這是在Visual Studio中非常相似。只需打開原始輸出選項卡,然後轉到最後一個note,其中可能包含真實來源的錯誤。


MSVC結果與rextestersource_file.cpp的最後一個註釋再次指出了錯誤的真正來源。

+0

我真的很感謝你。解決你的答案 –

+0

@HumamHelfawi De nada ;-) – user0042