2012-07-06 70 views
0

時,我有以下模板函數:奇怪的錯誤實例化模板函數

template <std::size_t first, std::size_t last, typename T> 
bool in_range(T& in) 
{ 
    for(auto i = in.begin(); i!=in.end(); ++i) 
     if(*i<first || *i>last) 
      return false; 
    return true; 
} 

但是當我嘗試使用它作爲這樣的:

std::vector<int> test; 
test.push_back(1); 
test.push_back(5); 
test.push_back(6); 

std::cout<<in_range<4,7>(test); 

我得到這個奇怪的錯誤:

main.cpp: In instantiation of 'bool in_range(T&) [with long long unsigned int first = 4ull; long long unsigned int last = 7ull; T = std::vector<int>]': 
main.cpp:31:34: required from here 

我在做什麼錯了?

編輯:全力打造日誌:http://pastebin.com/Cwemq2Hk

+1

這真的是完整的錯誤信息? – 2012-07-06 15:07:25

+0

這是錯誤消息的一部分,說明發生錯誤的位置。它會被跟隨或之前的錯誤描述 - 你能發佈完整的消息嗎? – 2012-07-06 15:09:21

+1

編譯精美VC2010,在這裏http://ideone.com/yGh30 – hmjd 2012-07-06 15:09:57

回答

2

如果我構建與啓用C++ 11的支持,然後再編譯。 Here is a demonstation

在C++ 11之前,auto有不同的含義,所以auto i = ...無效 - 它聲明瞭一個沒有類型的變量。

我想你使用的是GCC;根據版本的不同,您需要指定-std=c++0x-std=c++11作爲命令行選項。

+0

已經啓用了C++ 11 – user1233963 2012-07-06 15:15:04

+1

好的,在這種情況下,我們需要知道編譯器的版本,這將有助於獲得一個完整的可編譯示例。您的代碼適用於我:http://ideone.com/e4Qjh – 2012-07-06 15:16:25