2013-12-16 2 views
4

부스트 :: 변형 및 향상을위한이 간단한 예제 코드 :: apply_visitor : 부스트 버전을 사용하는 맥 OS X 매버릭스에부스트 변형 apply_visitor 컴파일 오류

g++-mp-4.8 -MMD -DBOOST_ALL_DYN_LINK -DBOOST_SPIRIT_USE_PHOENIX_V3 -Wall -std=c++11 -Os -O3 -g -I/o\ 
pt/local/include -I./ -c tools/t6.cpp -o tools/build/x86_64/objs/t6.o 
In file included from /opt/local/include/boost/variant/apply_visitor.hpp:16:0, 
       from /opt/local/include/boost/variant/detail/hash_variant.hpp:23, 
       from /opt/local/include/boost/variant/variant.hpp:37, 
       from /opt/local/include/boost/variant/recursive_variant.hpp:36, 
       from tools/t6.cpp:4: 
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp: In instantiation of 'typename Visi\ 
tor::result_type boost::apply_visitor(const Visitor&, Visitable&) [with Visitor = Printer; Visitabl\ 
e = boost::variant<ExprFalse, ExprTrue, ExprMaybe>(ExprTrue (*)()); typename Visitor::result_type =\ 
void]': 
tools/t6.cpp:35:47: required from here 
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp:76:43: error: request for member 'a\ 
pply_visitor' in 'visitable', which is of non-class type 'boost::variant<ExprFalse, ExprTrue, ExprM\ 
aybe>(ExprTrue (*)())' 
    return visitable.apply_visitor(visitor); 
             ^
/opt/local/include/boost/variant/detail/apply_visitor_unary.hpp:76:43: error: return-statement with\ 
a value, in function returning 'void' [-fpermissive] 
make: *** [tools/build/x86_64/objs/t6.o] Error 1 

:

#include <boost/variant/recursive_variant.hpp> 

struct ExprFalse; 
struct ExprTrue; 
struct ExprMaybe; 

typedef boost::variant< 
    ExprFalse, 
    ExprTrue, 
    ExprMaybe 
    > Expression; 

struct ExprFalse { }; 
struct ExprTrue { }; 
struct ExprMaybe { }; 

struct Printer : public boost::static_visitor<> 
{ 
public: 
    Printer(std::ostream& os) : m_os(os) { } 
    void operator()(ExprFalse const& expr) const { m_os << "False"; } 
    void operator()(ExprTrue const& expr) const { m_os << "True"; } 
    void operator()(ExprMaybe const& expr) const { m_os << "Maybe"; } 

private: 
    std::ostream& m_os; 
}; 

int main() 
{ 
    Expression e(ExprTrue()); 
    boost::apply_visitor(Printer(std::cout), e); 
    return 0; 
} 

다음과 같은 컴파일 오류를 생성합니다 1.55.0.

내 인생에서 나는 그 문제를 파악할 수 없다. 나는 실제로 반환 형식 (비록 인쇄 방문자가 하나도 필요하지 않지만) 시도했지만, 나는 같은 오류로 끝났다.

어떤 통찰력도 인정 될 것입니다.

답변

6

당신은 most vexing parse 규칙에 의해 명중된다 : e는 실제로 기능이다. 추가 괄호 쌍을 추가하십시오.

Expression e((ExprTrue())); 
+0

{} 초기화가 C++ 11에 추가 된 이유는 무엇입니까? 표현식 e {ExprTrue()}; –

+1

@Ben Hobbs 일반적으로 다양한 초기화 양식을 통일하기 위해 추가되었습니다. –

+0

@Igor R. 고맙습니다. 나는 길을 벗어난 길이었고, 이로 인해 많은 시간과 악몽을 덜어 줬습니다. – RandomBits