功能静态成员变量

功能静态成员变量

本文介绍了的boost ::功能静态成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来存储一个boost ::函数作为静态变量,其中参考?是从一个函数取得。

I'm trying to store a boost::function as a static variable where the "reference?" is fetched from a function.

变量是这样的:

static boost::function<void( const wchar_t*, const bool)> s_logMessage;

当我尝试编译我的错误:

When I try to compile I get the error:

错误LNK2001:解析外部符号?市民:静态类boost ::功能Gorbatras_Converter :: ConverterApp :: s_logMessage(s_logMessage @ ConverterApp @ Gorbatras_Converter @@ 2V $ @功能$$ A6AXPB_W_N @ Z @提振@@一)

我注意到,如果我不使用变量,我可以编译它,而不该错误(猜这是一个编译器的优化,因为它没有使用),但只要我尝试使用s_logMessage变量我得到的链接错误

I've noticed that if I don't use the variable I can compile it without that error (guess it's a compiler optimization since it's not used) but as soon as I try to use the s_logMessage variable I get the linker error.

我的设置s_logMessage的方式是这样的:

My way of setting s_logMessage looks like this:

const int ConverterApp::RunConverter( boost::function<void( const wchar_t* a_message, const bool a_newLine)> a_logMessage )
{
    ConverterApp::s_logMessage = a_logMessage;
    ...
}

我试着将它设置为一个非静态成员变量,然后它似乎工作。但我需要它作为静态的,否则我可能只是一起藏汉把它作为,因为它的将是一个很大的功能,我不希望有一个参数。

I've tried setting it as a non-static member variable and then it appears to work. But I need it as static, otherwise I might just aswell send it along as a parameter which I don't want as it's going to be a lot of functions.

如果你想知道为什么我发送日志功能为参数,这是因为它来自一个单独的程序。

If you wonder why I'm sending a log function as parameter, that is because it comes from a separate program.

所以我需要帮助的是我怎么救了boost :: function函数在一个静态变量?

So what I need help is how do I save the boost::function function in a static variable?

推荐答案

您错过一个的定义的为静态数据成员(你只有一个的声明的那成员类定义)。将下面的命名空间范围:

You're missing a definition for the static data member (you only have a declaration of that member in your class definition). Put the following at namespace scope:

boost::function<void( const wchar_t*, const bool)> ConverterApp::s_logMessage;

如果你从来不使用数据成员(从技术上讲,如果不是的 ODR使用的的,看到C ++ 11标准第3.2),则不需要定义 - 因此行为你正在观察。

If you never use the data member (technically, if it is not odr-used, see paragraph 3.2 of the C++11 Standard), then the definition is not needed - hence the behavior you are observing.

这篇关于的boost ::功能静态成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:28