Thread: No Match for fstream::write().

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Question No Match for fstream::write().

    No Match for fstream::write().
    I am using Mingw gcc compiler and when I compiled this code I got an error:
    Code:
    mand.cpp:38: no matching function for call to `std::basic_fstream<char, 
       std::char_traits<char> >::write(unsigned char[100], const int&)'
    E:/MinGW/include/c++/3.2.3/bits/ostream.tcc:387: candidates are: 
       std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, 
       _Traits>::write(const _CharT*, int) [with _CharT = char, _Traits = 
       std::char_traits<char>] <near match>
    The line was :
    Code:
    fstream file("IMAGE.RAW", ios::out|ios::binary);
    if(!file){
        cout<<"\nError Opening file...\n\n";
        return 1;
    }
    for(int i=0; i<SIZE; ++i)
        file.write(Canvas[ i], SIZE); //this line
    file.close();
    cout<<"\nFILE WRITTEN\n";
    return 0;
    }
    /*
    where
    unsigned char Canvas[SIZE][SIZE]={'\0'};
    const int SIZE = 100; //global;
    */
    I am not sure about the cause of the error;
    This function worked in my prestandard compiler in some other program.
    So what function should I use to achieve a similar purpose?
    And what's with all the'<>' symbols in the error report? I did'nt know that fstream was 'templated'

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    file.write(reinterpret_cast<char*>(Canvas[i]), 100);

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Thak you. It works.
    Can you tell me why there was no match even though the variable was unsigned char?
    Conversion from unsigned char to char is quite harmless and why did'nt the compiler implicitly do it? And why doesn't the static_cast work?
    Code:
    invalid static_cast from type `unsigned char[100]' to type `char*'
    I got this error. It is stupid because I want the compiler to implicitly convert 'unsigned char*[100]' and not 'unsigned char[100]'.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    32
    Quote Originally Posted by arjunajay
    Thak you. It works.
    Can you tell me why there was no match even though the variable was unsigned char?
    Conversion from unsigned char to char is quite harmless and why did'nt the compiler implicitly do it? And why doesn't the static_cast work?
    Code:
    invalid static_cast from type `unsigned char[100]' to type `char*'
    I got this error. It is stupid because I want the compiler to implicitly convert 'unsigned char*[100]' and not 'unsigned char[100]'.

    Actually unsigned characters and signed characters have a big difference. Most modern compilers (VC++ for example) have the /j option set in the compiler, which forces all chars to be set to unsigned. However a signed character can only go from values -128 to 128, and unsigned is 0 to 255. So it isn't as harmless as you might think.

    Secondly, fwrite accepts a char* not an unsigned char* or an unsigned char, and it will not do the conversion for you without telling it to, i didn't realize static_cast wouldn't work, but static_cast in general is unsafe unless you are 99% sure that you won't run into a logic error.

    As for the error you recieved, unsigned char[100] isn't a proper data type, its just an array of unsigned chars. You would need to do a cast from an unsigned char* to a char*.

    I am not quite clear on what your trying to do in your last statement there. You want to convert an array of 100 unsigned char* to char*?

  5. #5
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    I am not quite clear on what your trying to do in your last statement there. You want to convert an array of 100 unsigned char* to char*?
    No,
    The variable is unsigned char canvas[100][100];
    so canvas [100] or canvas [ i] means an array of unsigned char, but my compiler is reporting it as char[100] and not char*[100]. that is what puzzles me...
    invalid static_cast from type `unsigned char[100]' to type `char*'
    this was the error when I used static cast
    shouldn't it be
    invalid static_cast from type `unsigned char*[100]' to type `char*'
    P.S. Kindly ignore this post if it does'nt make sense.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 06:37 PM
  2. no match for 'operator>>'
    By Taka in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2009, 12:17 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. 2 array match
    By ajastru2000 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2003, 07:58 AM