C++ 掌握C++;函数语法
我无法将消息从DLL的日志函数发送到客户端的日志函数。 我可以使用这样的函数指针: 动态链接库:C++ 掌握C++;函数语法,c++,api,dll,std,C++,Api,Dll,Std,我无法将消息从DLL的日志函数发送到客户端的日志函数。 我可以使用这样的函数指针: 动态链接库: 类内部记录器 { 公众: 静态std::函数loggerCallback; 静态std::stringstream logMsg; 静态int logSend(LogLevel LogLevel); }; void设置日志函数(const std::function和logFunction) { InnerLogger::loggerCallback=logFunction; } 客户: voi
类内部记录器
{
公众:
静态std::函数loggerCallback;
静态std::stringstream logMsg;
静态int logSend(LogLevel LogLevel);
};
void设置日志函数(const std::function和logFunction)
{
InnerLogger::loggerCallback=logFunction;
}
客户:
void outerLogger(const std::string& message, int level)
{
// display/save messages
}
main()
{
std::function<void(const std::string& message, int level)> logFunctionPtr = outerLogger;
setLoggingFunction(logFunctionPtr);
}
class OuterLogger
{
public:
OuterLogger();
~OuterLogger();
void logFunction(const std::string& message, int level);
};
void OuterLogger::logFunction(const std::string& message, int level)
{
// display/save messages
}
main()
{
OuterLogger outerLogger;
std::function<void(const std::string& message, int level)> logFunctionPtr = outerLogger.logFunction;
setLoggingFunction(logFunctionPtr);
}
void outerLogger(常量标准::字符串和消息,整数级)
{
//显示/保存消息
}
main()
{
std::function logptr=outerLogger;
设置日志功能(日志功能PTR);
}
我可以使用InnerLogger::logSend,outerLogger将其拾取
但当“外部”记录器是类成员时,我做错了什么:
(DLL不变)
客户:
void outerLogger(const std::string& message, int level)
{
// display/save messages
}
main()
{
std::function<void(const std::string& message, int level)> logFunctionPtr = outerLogger;
setLoggingFunction(logFunctionPtr);
}
class OuterLogger
{
public:
OuterLogger();
~OuterLogger();
void logFunction(const std::string& message, int level);
};
void OuterLogger::logFunction(const std::string& message, int level)
{
// display/save messages
}
main()
{
OuterLogger outerLogger;
std::function<void(const std::string& message, int level)> logFunctionPtr = outerLogger.logFunction;
setLoggingFunction(logFunctionPtr);
}
类外部记录器
{
公众:
OuterLogger();
~OuterLogger();
void logFunction(const std::string&message,int级别);
};
void OuterLogger::logFunction(常量std::字符串和消息,int级别)
{
//显示/保存消息
}
main()
{
外部记录器外部记录器;
std::function logFunctionPtr=outerLogger.logFunction;
设置日志功能(日志功能PTR);
}
下面是一个如何将类绑定到函数的示例:
#include <iostream>
#include <functional>
class A {
public:
int x = 1;
void func() {
std::cout << x;
}
~A() {
x = 2; // just to test the destructor
}
};
int main()
{
std::function<void(void)> f;
{
A a;
f = std::bind(&A::func, a); // do not bind &a because it will be out of scope
}
f();
return 0;
}
std::function<void(const std::string&, int)> logFunctionPtr =
[outerLogger](const std::string& message, int level) mutable {
outerLogger.logFunction(message, level);
};
#包括
#包括
甲级{
公众:
int x=1;
void func(){
std::cout我尝试了几种不同的传递函数指针的方法,但没有一种有效,但结果证明该代码没有任何错误
当我使用DLL记录器成功接受的任何形式的代码时,调试显示它会立即变为null
我正在使用VS的测试资源管理器。结果发现一些测试的进程实例没有正确关闭,所以我在后台卡住了testhost进程,我认为地址不知怎么搞砸了
线索是间歇性无法多次运行测试,以及在调试模式下出现非常奇怪的行为-执行标记会向后移动和跳跃,并产生神秘的WINRT_ASSERT(!is_sta())异常。答案是运行Process Explorer并在VS下检查卡住的进程。(有点恼人的是,VS无法告诉我它正在测试的过程存在问题,但nvm)
我的代码使用WinML库。如果我在CPU模式下运行就可以了。如果我使用GPU,它会在关机时卡住,所以我需要找出原因
谢谢大家的帮助
编辑:我在问题中的最初假设是,将代码从一个非对象移动到另一个对象是一个问题,这是完全错误的。代码的原始非对象版本之所以有效,是因为它在一个演示客户机中,一个调用DLL的可执行控制台程序,而该程序从未受到狡猾的关机问题的影响。在单元测试中如果有多个实例,这就成了一个问题。“…但是当'outer'记录器是类成员时,我做了一些错误:…”什么错误你明白了吗?你的问题是什么?成员函数有一个隐式的this
指针作为第一个参数,所以OuterLogger::logFunction
与你的参数没有相同的签名。@eike这不是问题,std::function
处理它。会有帮助吗?谢谢Victor。我在别处看到它时尝试过这个,但是我得到:错误C2440'initializing':无法从'std::_Binder'转换为'std::function'我的错,我忘记了占位符。这起作用:std::string str;//占位符满足bind int x;//占位符满足bind logFunctionPtr=std::bind(&TestLogger::logFunction,this,str,x);
并作为std::function使用。或者它看起来是。奇怪的是,它会被函数接受并设置为var,但一旦setter超出范围,var就会返回null,这是它以前从未做过的。我已经放弃了这一点,现在让血腥的东西成为全局静态的。(你如何在注释中添加新行?)使用*this而不是this。问题是它只是一个指针,当类超出范围时,整个结构将崩溃。另外,请确保您有一个副本构造函数。