2012-10-18 40 views
3

我正面臨與鐺3.1的問題。 GCC 4.2不會出現這個特殊問題。下面是所發生的錯誤的示例:鐺錯誤,沒有可行的轉換

#include <stdio.h> 
#include <iostream> 
#include <sstream> 
#include <string> 

typedef unsigned short char16_t; 
typedef char16_t TCHAR; 

typedef std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > wstringT; 

template<class T> 
inline wstringT MyTestFunction(const T& source) 
{ 
std::wstringstream out; 
out << source ; 
return out.str(); // error occurs here 
}; 

該錯誤消息指出:

No viable conversion from '__string_type' (aka 'basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >') to 'wstringT' (aka 'basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> >') 

的代碼被編譯與所述編譯器標記-fshort-WCHAR,這是應該wchar_t的轉換爲一個16位無符號短路。我正在編譯XCode v4.3.2上的代碼。

+2

爲什麼不使用'wchar_t'爲'TCHAR'? 'wchar_t'可以或不可以和'unsigned short'又名'char16_t'相同。 – Vlad

回答

3

如果你想使用TCHAR你必須讓每個模板擴大使用它包括wstringstream你真正想要一個basic_stringstream<TCHAR>或者你可以:

typedef std::basic_stringstream<TCHAR> tstringstream;