Help filling a structure with data from a file.

This is a discussion on Help filling a structure with data from a file. within the C++ Programming forums, part of the General Programming Boards category; >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 ...

  1. #31
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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

  2. #32
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Which line? I'm rather confused!!

    With this code -

    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;
    }
    code, I get this result -

    Name:  thing.JPG
Views: 232
Size:  14.6 KB

    from this text file -

    253.99,4478,Video Card,1
    88.99,7789,Motherboard,3
    154.99,4753,CPU,2
    Limitations - just one info variable, not an array.
    Last edited by twomers; 09-27-2006 at 03:16 PM.

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> 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 03:18 PM.

  4. #34
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    [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;
    }

  5. #35
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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" );

  6. #36
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Just get rid of that line It should work without it. It's the throw line, right?

  7. #37
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Lightbulb

    Quote Originally Posted by twomers
    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.

  8. #38
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Does it work without the exception?

  9. #39
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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().

  10. #40
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

  11. #41
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    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:
    Code:
    outDataFile.open("place", ios::out | ios::binary);
    outDataFile.write(reinterpret_cast<char *>(p), sizeof(p));
    to read:
    Code:
    inDataFile.open("place", ios::in | ios::binary);
    inDataFile.read(reinterpret_cast<char *>(p), sizeof(p));
    heh, too simple.

  12. #42
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 07:43 AM
  2. Replies: 3
    Last Post: 02-26-2008, 02:12 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 07:09 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21