0

I tried to create function that get pointer for the functions of binary file, write and read. Both of functions need get :

1)char* (on write function the char* mean from witch address write, and on read function char* mean , to witch address read)

2) int (on write address that meam how many wirte and on read function that mean how many read).

I want create function that get pointer for read/ write , but i get an error my code.

#include <iostream>
using namespace std;
#include <fstream>

void func(void(*funcToCall)(char*, int))
{
    char * temp= "zzz";
    funcToCall(temp, 3);
}
int main()
{
    fstream dskfl;
    dskfl.open("aa.txt", ios::out |  ios::binary);
    dskfl.close();
    dskfl.open("aa.txt", ios::out | ios::in | ios::binary);

    char ddd[]= "fffff";
    dskfl.write((char*)ddd, 5);
    dskfl.seekp(0);

    func(dskfl.write);
    dskfl.close();
}

i get error on line

func(dskfl.write);

Error   1   error C3867: 'std::basic_ostream<char,std::char_traits<char>>::write': function call missing argument list; use '&std::basic_ostream<char,std::char_traits<char>>::write' to create a pointer to member 

why please?

2
  • You need two pieces of information to call a method of a class - instance of the class and the method to call. fstream* instance = &dskfl; and void (fstream::*method)(char*, int) = &fstream::write; and then use (instance->*method)(char*, int) to call the method. Commented Mar 16, 2016 at 7:06
  • @Vishal i changed to "void func(fstream* instance, void(fstream::*method)(char*, int) )" and call of function like func(&dskfl,&( fstream::write)); i get error 4 IntelliSense: argument of type "std::basic_ostream<char, std::char_traits<char>> &(std::basic_ostream<char, std::char_traits<char>>::*)(const char _Str, std::streamsize _Count)" is incompatible with parameter of type "void (std::basic_fstream<char, std::char_traits<char>>::)(char *, int)" Commented Mar 16, 2016 at 7:21

1 Answer 1

0

you can use a lambda

for example:

change the signature of your outer func for receive a std::function:

void func(std::function<void(char*, int)>&& funcToCall)

and call it like this:

func([&dskfl](char* c, int i){ dskfl.write(c, i); });
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.