Thursday, April 28, 2011

Standard POSIX read shadowed by a read method with different signature

I have a C++ File class with read function, that is supposed to read whole contents of a file (just like Python does) into a buffer. However, when I tried to call read function from unistd.h, I get:

file.cpp:21: error: no matching function for call to ‘File::read(int&, char*&, int)’

file.cpp:17: note: candidates are: char* File::read()

What am I doing wrong? These have completely different signatures, why can't I simply call it?

From stackoverflow
  • Have you tried being explicit about scope;

    char* File::read()
    {
       // Double-colon to get to global scope
       ::read(...);
       // ..
    }
    

    ?

    gruszczy : Thanks. This was exactly what I needed. Too much Python, I must go back to C++ and get this all back again :-)
  • The definition for the posix standard version of the read method is defined as extern "C". This is neccesary so that the read symbol is not mangled by the C++ compiler and links against the proper function in the library. Mixing and matching C and C++ symbols will have unpredictable results. If possible, rename the c++ function so as not to conflict with any symbols that are declared extern "C".

0 comments:

Post a Comment