2010-07-27 58 views
0
1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1194): error C2451: conditional expression of type 'void' is illegal 
1>   Expressions of type void cannot be converted to other types 
1>   C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\list(1188) : while compiling class template member function 'void std::list<_Ty>::remove(const _Ty &)' 
1>   with 
1>   [ 
1>    _Ty=ServerLoginResponseCallback 
1>   ] 
1>   c:\users\shawn\edu\csclient\ConfigurationServerClient.h(56) : see reference to class template instantiation 'std::list<_Ty>' being compiled 
1>   with 
1>   [ 
1>    _Ty=ServerLoginResponseCallback 
1>   ] 

這裏是產生錯誤代碼......MSVC 2010個模板編譯問題

typedef std::shared_ptr<protocols::ServerLoginResponse> ServerLoginResponsePtr; 
typedef std::function<void (ServerLoginResponsePtr)> ServerLoginResponseCallback; 
typedef std::list<ServerLoginResponseCallback> ServerLoginResponseCallbackList; 

因此,我們必須返回void,並且類型的shared_ptr的參數仿函數列表。有誰知道爲什麼MSVC編譯器有問題嗎?

+0

的錯誤信息是不從三個typedef中,它呢?他們沒有實例化std :: list << ServerLoginResponseCallback> :: remove(ServerLoginResponseCallback const&);' – MSalters 2010-07-28 09:03:49

回答

1

看來你有實例化的問題。我剛剛嘗試重現您的錯誤,但我的MSVC成功編譯了此代碼。

請向我們展示更多代碼))例如,向我們展示如何在創建後使用此列表。

+0

這就是問題所在。我有一個刪除功能,沒有辦法比較Functors。我將不得不更多地研究如何管理Functor集合。 – shaz 2010-07-28 14:39:03

3

在編譯類模板成員函數 '無效的std ::列表< _Ty> ::刪除(常量_Ty &)' 與[_Ty = ServerLoginResponseCallback]

您實例std::list<std::function<void (ServerLoginResponsePtr)>>,並試圖調用就可以了erase,並且依賴於調用兩個std::function對象operator==,但std::function s爲沒有可比性(只nullptr):

§20.8.14.2[func.wrap.func](從最後草案n3092):

成員函數:

// deleted overloads close possible hole in the type system 
template<class R2, class... ArgTypes2> 
bool operator==(const function<R2(ArgTypes2...)>&) = delete; 

template<class R2, class... ArgTypes2> 
bool operator!=(const function<R2(ArgTypes2...)>&) = delete; 

這些都是免費的功能:

template<class R, class... ArgTypes> 
bool operator==(const function<R(ArgTypes...)>&, nullptr_t); 

template<class R, class... ArgTypes> 
bool operator==(nullptr_t, const function<R(ArgTypes...)>&); 
+0

謝謝大衛。有關管理std :: function對象集合的任何提示? – shaz 2010-07-28 14:40:12

+0

爲了您的特定目的,我只推薦使用'boost :: signal'(或'boost :: signal2')。他們的解決方案IIRC正在爲每個函數註冊返回一個令牌。該令牌稍後可用於斷開客戶端連接。 – 2010-07-28 14:43:53