>It was this code, wasn't it
Yes, I'm getting this error: no matching function for call to `std::bad_cast::bad_cast(const char[19])'
bad_cast seems to exist, because it says: candidates are
>It was this code, wasn't it
Yes, I'm getting this error: no matching function for call to `std::bad_cast::bad_cast(const char[19])'
bad_cast seems to exist, because it says: candidates are
Which line? I'm rather confused!!
With this code -
code, I get this result -Code:#include <algorithm> #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <windows.h> // Your struct struct Info { double price; int serialNum; std::string descript; int order; }; // Templated type-cast function template <typename T, typename U> T lexical_cast ( U src ) { std::stringstream s; T dst; if ( !( s << src ) || !( s >> dst ) ) throw std::bad_cast ( "Invalid conversion" ); return dst; } int main ( void ) { std::ifstream in ( "thing.txt" ); if ( !in ) { in.close(); return 1; } std::string temp; Info info; while ( getline( in, temp, ',' ) ) { info.price = lexical_cast< double, std::string > ( temp ); std::cout<< '\n' << "Price: " << info.price; getline( in, temp, ',' ); info.serialNum = lexical_cast< int, std::string > ( temp ); std::cout<< '\n' << "Serial: " << info.serialNum; getline( in, temp, ',' ); info.descript = temp; std::cout<< '\n' << "Description: " << info.descript; getline( in, temp, '\n' ); info.order = lexical_cast< int, std::string > ( temp ); std::cout<< '\n' << "Order #: " << info.order << '\n'; } in.close(); return 0; }
Attachment 6811
from this text file -
Limitations - just one info variable, not an array.253.99,4478,Video Card,1
88.99,7789,Motherboard,3
154.99,4753,CPU,2
Last edited by twomers; 09-27-2006 at 02:16 PM.
>> std::bad_cast ( "Invalid conversion" );
This is non-standard (18.5.2). There is no bad_cast constructor that takes a string (C or C++ style). Some implementations might allow it.
Last edited by Daved; 09-27-2006 at 02:18 PM.
[Sorry for posting so much code, but this is the main loop of a vector implementation of the problem. Same functions/struct etc as before]
Code:int main ( void ) { std::ifstream in ( "thing.txt" ); if ( !in ) { in.close(); return 1; } std::string temp; Info info; std::vector<Info> infoVec; while ( getline( in, temp, ',' ) ) { info.price = lexical_cast< double, std::string > ( temp ); getline( in, temp, ',' ); info.serialNum = lexical_cast< int, std::string > ( temp ); getline( in, temp, ',' ); info.descript = temp; getline( in, temp, '\n' ); info.order = lexical_cast< int, std::string > ( temp ); infoVec.push_back( info ); } in.close(); for ( int i=0; i!=(int)infoVec.size(); i++ ) { std::cout<< '\n' << i << " :: Price: " << infoVec[i].price; std::cout<< '\n' << i << " :: Serial: " << infoVec[i].serialNum; std::cout<< '\n' << i << " :: Description: " << infoVec[i].descript; std::cout<< '\n' << i << " :: Order: " << infoVec[i].order << '\n'; } return 0; }
>This is non-standard (18.5.2). There is no bad_cast constructor that takes a string (C or C++ style).
Is there any way to rewrite that line? The compiler shows a candidate with a bad_cast as the argument:
C:/program files/Dev-Cpp/include/c++/3.3.1/typeinfo:136: error: candidates are:
std::bad_cast::bad_cast(const std::bad_cast&)
I'll post the line so others readers know.
Code:throw std::bad_cast ( "Invalid conversion" );
Just get rid of that line It should work without it. It's the throw line, right?
Yes, the throw line. Or I suppose I could change it to some other std::exception.Originally Posted by twomers
Does it work without the exception?
Use another exception if you want to specify text, or just throw a bad_cast without any error message (throw std::bad_cast();). A compiler-generated one will be displayed if you call what().
>Does it work without the exception?
Yes.
>or just throw a bad_cast without any error message (throw std::bad_cast();).
Ok, I did this, and the code now compiles and runs.
hey guys! I was just flipping through my text book and found a simple solution to this problem(as in not using STL/vectors)
Binary files!
to write:
to read:Code:outDataFile.open("place", ios::out | ios::binary); outDataFile.write(reinterpret_cast<char *>(p), sizeof(p));
heh, too simple.Code:inDataFile.open("place", ios::in | ios::binary); inDataFile.read(reinterpret_cast<char *>(p), sizeof(p));
What does your Info struct look like? That won't work on all structs (it won't work on twomers example).
>> found a simple solution to this problem(as in not using STL/vectors)
What seems simpler now won't necessarily be simpler in the long run. Part of the reason strings and vectors are recommended are because they are actually simpler in the long run.