2011-10-26 64 views
0

我遇到了boost :: lexical_cast的問題。我正嘗試在GLM(OpenGL數學)庫中使用它。如何讓Boost.LexicalCast正常工作?

要允許詞彙鑄造,我已經實現了運營商< <功能的類問題:

template <class T> 
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec) 
{ 
    out << vec.x << " " << vec.y; 
    return out; 
} 

template <class T> 
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec) 
{ 
    in >> vec.x; 
    in >> vec.y; 
    return in; 
} 

我測試過像這樣的運營商:

std::cout << glm::ivec2(1, 1) << glm::vec2(1.0f, 1.0f); 

和:

std::stringstream ss("640 480"); 
glm::ivec2 pt; 
ss >> pt; 
std::cout << pt << std::endl; 

這工作得很好,但是如果我試試這個:

glm::ivec2 pt = boost::lexical_cast<glm::ivec2>("1 1"); 

我得到以下錯誤:

/usr/include/boost/lexical_cast.hpp: In member function ‘bool boost::detail::lexical_stream_limited_src<CharT, Base, Traits>::operator>>(InputStreamable&) [with InputStreamable = glm::detail::tvec2<int>, CharT = char, Base = std::basic_streambuf<char>, Traits = std::char_traits<char>]’: 
/usr/include/boost/lexical_cast.hpp:1151:13: instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<B>::param_type, CharT*, std::size_t) [with Target = glm::detail::tvec2<int>, Source = const char*, bool Unlimited = false, CharT = char, typename boost::call_traits<B>::param_type = const char* const, std::size_t = long unsigned int]’ 
/usr/include/boost/lexical_cast.hpp:1174:77: instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = glm::detail::tvec2<int>, Source = char [8]]’ 
test2.cpp:41:59: instantiated from here 
/usr/include/boost/lexical_cast.hpp:785:29: error: cannot bind ‘std::basic_istream<char>’ lvalue to ‘std::basic_istream<char>&&’ 
/usr/include/c++/4.6/istream:852:5: error: initializing argument 1 of ‘std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char, _Traits = std::char_traits<char>, _Tp = glm::detail::tvec2<int>]’ 

編輯:似乎只有在包括了Boost.PropertyTree頭髮生錯誤。

回答

1

答案被另一個發現,所以我會離開這裏。

他提到一些有關參數依賴查找,並建議流運營商放置在GLM ::詳細namepace像這樣:

namespace glm { 
namespace detail { 

template <class T> 
std::ostream& operator<<(std::ostream& out, const glm::detail::tvec2<T>& vec) 
{ 
    out << vec.x << " " << vec.y; 
    return out; 
} 

template <class T> 
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec) 
{ 
    in >> vec.x; 
    if (in.good() && in.ignore(256, ' ').good()) 
     in >> vec.y; 
    return in; 
} 

}} // glm::detail 

在那之後,一切工作正常。

1

您發佈此提取操作:

template <class T> 
std::istream& operator>>(std::istream& in, glm::detail::tvec2<T>& vec) 

但錯誤意味着它正試圖與這個編譯:

std::istream& std::operator>>(std::istream&&, _Tp&) 
[with ... _Tp = glm::detail::tvec2<int>] 

你確定你貼的比賽你什麼編譯的代碼? (如果你不能馬上看到它,請關注操作員的第一個參數)。


OK,這個完整的代碼工作正常,我:

#include <sstream> 
#include <iostream> 

#include <boost/lexical_cast.hpp> 

template <class T> 
struct tvec2 { 
    tvec2() : x(), y() {} 
    tvec2(tvec2 const &) = default; 
    tvec2(T x_, T y_) : x(x_), y(y_) {} 

    T x; 
    T y; 
}; 

template <class T> 
std::ostream& operator<<(std::ostream& out, const tvec2<T>& vec) 
{ 
    out << vec.x << " " << vec.y; 
    return out; 
} 

template <class T> 
std::istream& operator>>(std::istream& in, tvec2<T>& vec) 
{ 
    // yuck, boost disables skipws on the input stream 
    in >> vec.x; 
    if (in.good() && in.ignore(256, ' ').good()) 
     in >> vec.y; 
    return in; 
} 

void test_operators() 
{ 
    tvec2<int> intvec(2,3); 
    std::cout << "intvec = {" << intvec << "}\n"; 

    std::stringstream ss; 
    ss << intvec; 

    tvec2<int> dupvec; 
    ss >> dupvec; 
    std::cout << "dupvec = {" << dupvec << "}\n"; 
} 

void test_lexical_cast() 
{ 
    std::cout << "and now with lexical_cast ...\n"; 
    tvec2<int> dupvec = boost::lexical_cast<tvec2<int> >("2 3"); 
    std::cout << "dupvec = {" << dupvec << "}\n"; 
} 

int main() 
{ 
    test_operators(); 
    test_lexical_cast(); 
} 
+0

是的,我絕對相信我發佈的代碼就是我正在編譯的代碼。 –

+0

好的......你是否在'boost :: lexical_cast'之外使用提取('>>')運算符來處理代碼?您只顯示測試插入運算符的代碼。 – Useless

+0

我將它添加到我原來的帖子中。 –