C2491:'std :: numpunct< _Elem> :: id':dllimport定义不允许静态数据成员 [英] C2491: 'std::numpunct<_Elem>::id' : definition of dllimport static data member not allowed

查看:874
本文介绍了C2491:'std :: numpunct< _Elem> :: id':dllimport定义不允许静态数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据以下代码,

#include <sstream>
#include <stdint.h>

template <typename D> void func() {
    std::basic_stringstream<D> outStream;
    D suffix = 0;
    outStream << suffix;
}

void main() {
    func<char>();     // OK
    func<wchar_t>();  // OK
    func<uint16_t>(); // generates C2491
}

以下编译错误是什么意思?

what does following compile error mean?


错误C2491:'std :: numpunct< _Elem> :: id':不允许dllimport静态数据成员的定义

error C2491: 'std::numpunct<_Elem>::id' : definition of dllimport static data member not allowed


推荐答案

您不能使用

_declspec(dllimport)

并提供定义。

限定符告诉编译器函数是从一个不同的库导入的,而不是你现在正在编译的库,因此为它提供一个定义是没有意义的。

The qualifier tells the compiler that the function is imported from a different library than the one you are compiling now, so it wouldn't make sense to provide a definition for it.

包含标题时,限定符应为

When including the header, the qualifier should be

_declspec(dllimport)

,当您编译提供方法定义的模块时,应该是:

and when you are compiling the module that provides a definition for the method it should be:

_declspec(dllexport)



<通常的做法是:

The usual way of doing this is:

#ifdef CURRENT_MODULE
#define DLLIMPORTEXPORT _declspec(dllexport)
#else
#define DLLIMPORTEXPORT _declspec(dllimport)
#endif

定义 CURRENT_MODULE 仅在包含定义的模块中定义,因此在编译该模块时,将导出该方法。所有包含头的其他模块没有定义 CURRENT_MODULE ,并且将导入该函数。

The define CURRENT_MODULE is only defined in the module that contains the definitions, so when compiling that module the method is exported. All other modules that include the header don't have CURRENT_MODULE defined and the function will be imported.

猜猜你的指令 - _declspecimport - 与此类似。

I'm guessing your directive - _declspecimport - is similar to this.

这篇关于C2491:'std :: numpunct&lt; _Elem&gt; :: id':dllimport定义不允许静态数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆