英文:
how to resolve the warning warning C4251: .... needs to have dll-interface to be used by clients of class 'TestClass'
问题
我创建了以下简单的类并构建它
```cpp
class __declspec(dllexport) TestClass
{
private:
std::unique_lock<std::mutex> lock;
public:
void doIt() {
// do something
}
};
但是我遇到了以下警告
1>C:\gitview\frm_multi\framework\appCpp\acout.h(48,35): 警告 C4251: 'TestClass::lock':类 'std::unique_lockstd::mutex' 需要具有 dll 接口,以便被 'TestClass' 类的客户端使用
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.29.30133\include\mutex(593): 消息:请参阅 'std::unique_lockstd::mutex' 的声明。
在我查找了一些文章之后,我将代码更新如下
template<> class __declspec(dllexport) std::unique_lock<std::mutex>;
class __declspec(dllexport) TestClass
{
private:
std::unique_lock<std::mutex> lock;
public:
void doIt() {
// do something
}
};
此时,我遇到了以下错误...
1>C:\gitview\frm_multi\framework\appCpp\acout.h(45,9): 错误 C2908: 显式特化;'std::unique_lockstd::mutex' 已经被实例化
如何解决这种情况?
我首先尝试的是将我的类暴露在 DLL 外部。
<details>
<summary>英文:</summary>
I create below simple class and build it
class __declspec(dllexport) TestClass
{
private:
std::unique_lock<std::mutex > lock;
public:
void doIt() {
// do something
}
};
but I met the below warning
1>C:\gitview\frm_multi\framework\appCpp\acout.h(48,35): warning C4251: 'TestClass::lock': class 'std::unique_lock<std::mutex>' needs to have dll-interface to be used by clients of class 'TestClass'
1>C:\Program Files\Microsoft Visual Studio22\Enterprise\VC\Tools\MSVC.29.30133\include\mutex(593): message : see declaration of 'std::unique_lock<std::mutex>'
after I googled some article, I updated my code like below
template<> class __declspec(dllexport) std::unique_lock<std::mutex>;
class __declspec(dllexport) TestClass
{
private:
std::unique_lock<std::mutex > lock;
public:
void doIt() {
// do something
}
};
at this moment, I met below error...
1>C:\gitview\frm_multi\framework\appCpp\acout.h(45,9): error C2908: explicit specialization; 'std::unique_lock<std::mutex>' has already been instantiated
how to solve this situation?
what I first try is just to expose my class outside DLL.
</details>
# 答案1
**得分**: 1
你可以这样消除警告:
```cpp
使用 PrivateData = 结构 {
std::unique_lock<std::mutex> lock;
};
类 __declspec(dllexport) TestClass {
private:
PrivateData data;
public:
void doIt() {
// 做一些事情
}
};
如果你的组件的用户不需要知道私有成员,你可以考虑只公开一个接口:
类 __declspec(dllexport) TestClass {
virtual void doIt() = 0;
};
类 TestClassImplementation: public TestClass {
private:
std::unique_lock<std::mutex> lock;
public:
void doIt() {
// 做一些事情
}
};
英文:
You can get rid of your warning like this:
using PrivateData = struct {
std::unique_lock<std::mutex > lock;
};
class __declspec(dllexport) TestClass {
private:
PrivateData data;
public:
void doIt() {
// do something
}
};
If users of your component don't need to know about private members, you can consider to expose only an interface :
class __declspec(dllexport) TestClass {
virtual void doIt() = 0;
};
class TestClassImplementation: public TestClass {
private:
std::unique_lock<std::mutex> lock;
public:
void doIt() {
// do something
}
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论