powered by simpleCommunicator - 2.0.59     © 2025 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / C++ [игнор отключен] [закрыт для гостей] / std::function
2 сообщений из 2, страница 1 из 1
std::function
    #39217477
crimeperson
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Слабо понимаю что ему нужно. (пример из википедии)

автор#include <iostream>
#include <functional>

struct A {
A(int num) : num_(num){}
void printNumberLetter(char c) const { std::cout << "Number: " << num_ << " Letter: " << c << std::endl; }
int num_;
};

int main()
{
// Содержит вызов метода класса.
std::function<void(const A&, char)> f_printA = &A::printNumberLetter;
A a(10);
f_printA(a, 'A');

}


Сообщения от компилятора:

автор1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1> main.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(506): error C2664: 'void std::_Func_class<_Ret,const A &,char>::_Set(std::_Func_base<_Ret,const A &,char> *)' : cannot convert argument 1 from '_Myimpl *' to 'std::_Func_base<_Ret,const A &,char> *'
1> with
1> [
1> _Ret=void
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,const A &,char>::_Do_alloc<_Myimpl,_Fret(__thiscall A::* const &)(char) const,_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,const A &,char>>
1> , _Fty=void (__thiscall A::* const &)(char) const
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,const A &,char>::_Do_alloc<_Myimpl,_Fret(__thiscall A::* const &)(char) const,_Alloc>(_Fty,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,const A &,char>>
1> , _Fty=void (__thiscall A::* const &)(char) const
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,const A &,char>::_Reset_alloc<_Fret,A,char,std::allocator<std::_Func_class<_Ret,const A &,char>>>(_Fret (__thiscall A::* const )(char) const,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,const A &,char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(442) : see reference to function template instantiation 'void std::_Func_class<_Ret,const A &,char>::_Reset_alloc<_Fret,A,char,std::allocator<std::_Func_class<_Ret,const A &,char>>>(_Fret (__thiscall A::* const )(char) const,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> , _Alloc=std::allocator<std::_Func_class<void,const A &,char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,const A &,char>::_Reset<void,A,char>(_Fret (__thiscall A::* const )(char) const)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,const A &,char>::_Reset<void,A,char>(_Fret (__thiscall A::* const )(char) const)' being compiled
1> with
1> [
1> _Ret=void
1> , _Fret=void
1> ]
1> c:\users\admin\documents\visual studio 2013\projects\project1\main.cpp(13) : see reference to function template instantiation 'std::function<void (const A &,char)>::function<void(__thiscall A::* )(char) const>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=void (__thiscall A::* )(char) const
1> ]
1> c:\users\admin\documents\visual studio 2013\projects\project1\main.cpp(13) : see reference to function template instantiation 'std::function<void (const A &,char)>::function<void(__thiscall A::* )(char) const>(_Fx &&)' being compiled
1> with
1> [
1> _Fx=void (__thiscall A::* )(char) const
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
...
Рейтинг: 0 / 0
std::function
    #39217542
Фотография NekZ
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
crimeperson,

Дурной пример, если честно. Хотя и компилируется на C++14 https://ideone.com/g0wdSI
Видимо, ваша стандартная библиотека не дотягивает :)
По-другому обычно пишут
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
#include <functional>
#include <iostream>

struct A {
A(int num) : num_(num){}
void printNumberLetter(char c) const { std::cout << "Number: " << num_ << " Letter: " << c << std::endl; }
int num_;
};

int main()
{
A a(10);
std::function<void(char)> f_printA = std::bind(&A::printNumberLetter, std::ref(a), std::placeholders::_1);
f_printA('A');
}


Но, bind -- нужен в очень редких случаях, рекомендуется использовать лямбды.
...
Рейтинг: 0 / 0
2 сообщений из 2, страница 1 из 1
Форумы / C++ [игнор отключен] [закрыт для гостей] / std::function
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]