부스트 :: 변형에서 int 값을 얻으려고합니다. 코드 생성 세분화 오류 - 왜? 오류를 생성하는 코드에 주석을 넣습니다. 나는 제대로 작동하지 않습니다부스트 :: 변종에서 세그먼트 화 오류를 생성하십시오.
int numberInt = boost::get<int>(v);
그래서 컴파일하지만 여전히 내가 int 값을 얻기 위해 관리하고 있지 않다
int *ptrInt = boost::get<int>(&v);
로 변경한다는 생각? 더블과 정확히 동일합니다. 문자열 유형이 작동 중입니다.
#include <iostream>
#include "boost/variant.hpp"
#include <boost/variant/get.hpp>
using namespace std;
int main(int argc, char* argv[])
{
boost::variant<int, double, std::string> v;
v = 16;
v = 3.1415;
v = "hello new year";
//int numberInt = boost::get<int>(v); //1) not working
//double numberDouble = boost::get<double>(v);//2) not working
int *ptrInt = boost::get<int>(&v); //3) compiling
if(ptrInt)
cout << *ptrInt << endl; //4) not displayed
//cout << *ptrInt << endl; //5) segmentation fault
double *ptrDouble = boost::get<double>(&v); //6) compiling
if(ptrDouble)
cout << *ptrDouble << endl; //7) not displayed
//cout << *ptrDouble << endl; //8) segmentation fault
std::string caption = boost::get<string>(v);
cout << caption << endl; //9) working
return 0;
}
// clear && clear && g++ test.cpp -std=c++11 -o test && ./test