#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
int main()
{
std::ofstream file("data.dat", std::ios_base::binary);
boost::archive::binary_oarchive oar(file);
return 0;
}
I compiled the above code with the following Makefile.
CC = g++
CFLAGS = -pedantic -std=c++11 -I/usr/local/Cellar/boost/1.54.0/include
LDFLAGS = -L/usr/local/Cellar/boost/1.54.0/lib -lboost_iostreams-mt -lboost_serialization-mt
all: test
test: test.o
$(CC) $(CFLAGS) test.o $(LDFLAGS) -o test
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o
When I run the program, it throws an instance of std::bad_cast
. The Problem lies in the call of boost::archive::binary_oarchive oar(file);
. I presume, file
is somehow not compatible with binary_oarchive
. I'm working on a system with Mac OS X 10.6 and used homebrew some hours ago to install boost. This sample code was extracted from the answer of Emile Cormier from:
Reading and writing C++ vector to a file
There, I found a solution for writing vector-data to a file.
Edit: Does anybody know, why this program throws an 'std::bad_cast'? Is it possible, that this code runs on other machines and the problem is related to my system?
Solution: To figure out, if my problem is related to some weird configuration error, I set up Linux Mint on another computer and tried to compile and run the above code there. Surprisingly, it didn't throw a "std::bad_cast". Then I uninstalled boost with homebrew and tried to compile the libraries with the instructions on the boost-Website and my preferred compiler gcc-4.9. After that, my code compiles and runs well. I think the problem was, that the boost-libraries I used, were compiled with another compilerversion than I use on my own code.