Testクラスを。で使用したいboost::lexical_cast
。オーバーロードoperator<<
しoperator>>
ましたが、ランタイムエラーが発生します。
これが私のコードです:
#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;
class Test {
int a, b;
public:
Test() { }
Test(const Test &test) {
a = test.a;
b = test.b;
}
~Test() { }
void print() {
cout << "A = " << a << endl;
cout << "B = " << b << endl;
}
friend istream& operator>> (istream &input, Test &test) {
input >> test.a >> test.b;
return input;
}
friend ostream& operator<< (ostream &output, const Test &test) {
output << test.a << test.b;
return output;
}
};
int main() {
try {
Test test = boost::lexical_cast<Test>("10 2");
} catch(std::exception &e) {
cout << e.what() << endl;
}
return 0;
}
出力:
bad lexical cast: source type value could not be interpreted as target
ところで、私はVisual Studio 2010を使用していますが、Fedora16をg++で試したところ、同じ結果が得られました。