У меня есть следующий фиктивный код для использования std :: function для создания вызываемого из указателя на член функции класса.C++ функция указатель на ошибку компиляции члена класса для std :: string, но ok для пользовательского класса
Я могу создать вызываемый пользовательский элемент String
класса empty
. Но когда я использую пустую строку std :: string, она имеет ошибку компиляции. Я пробовал как gcc-компилятор на компиляторе Xcode и ms на VS2015.
#include <string>
#include <vector>
#include <algorithm>
#include <numeric> //accumulate
#include <iterator> //inserter
#include <functional> //bind, function
struct String {
public:
bool empty() const {
return true;
}
};
int main() {
std::function<bool (const String&)> f = &String::empty; //OK
bool t = f(String()); //implicit parameter 'this' becomes explicit parameter
std::function<bool (const std::string&)> fcn = &std::string::empty; //Compile error
}
EDIT:
Я следую грунтовочной книге C++, и я стараюсь, чтобы достичь этого:
std::vector<std::string*> pvec; //use pointer for some reason
std::function<bool (const std::string*)> fcn = &std::string::empty;
std::find_if(pvec.begin(), pvec.end(), fcn);
std::find_if(pvec.begin(), pvec.end(), mem_fn(&std::string::empty));
std::find_if(pvec.begin(), pvec.end(), std::bind(&std::string::empty, std::placeholders::_1));
EDIT 2:
сообщениеошибки:
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::empty() const", referenced from:
_main in Source.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
EDIT 3: Я пробовал std :: vector, и он работает. std :: vector также работает
std::function<bool (const std::vector<int> &)> f2 = &std::vector<int>::empty;
bool t2 = f2(std::vector<int>());
std::cout << t2;
std::function<bool (const std::vector<std::string> &)> f3 = &std::vector<std::string>::empty;
bool t3 = f3(std::vector<std::string>());
Какая ошибка компиляции в первом случае –