C++ boost可添加和dll的可访问性错误
我正在WindowsVista上使用VisualStudio2008,并将函数转换为调试DLL 编译器错误: boost::operator模板出现可访问性错误:C++ boost可添加和dll的可访问性错误,c++,templates,visual-studio-2008,boost,dll,C++,Templates,Visual Studio 2008,Boost,Dll,我正在WindowsVista上使用VisualStudio2008,并将函数转换为调试DLL 编译器错误: boost::operator模板出现可访问性错误: error C2248: 'Field::Integer::Integer' : cannot access protected member declared in class 'Field::Integer' c:\program files\boost\boost_1_52_0\boost\operators.hpp(257
error C2248: 'Field::Integer::Integer' : cannot access protected member declared in class 'Field::Integer'
c:\program files\boost\boost_1_52_0\boost\operators.hpp(257) : while compiling class template member function 'Field::Integer boost::operator +(Field::Integer,const Field::Integer &)'
1> c:\program files\boost\boost_1_52_0\boost\operators.hpp(836) : see reference to class template instantiation 'boost::addable1<T,B>' being compiled
1> with
1> [
1> T=Field::Integer,
1> B=boost::detail::empty_base<Field::Integer>
1> ]
1> see reference to class template instantiation 'boost::addable<T>' being compiled
1> with
1> [
1> T=Field::Integer
1> ]
1> see reference to class template instantiation Field::Numeric<Value_Type,Descendant_Class>' being compiled
1> with
1> [
1> Value_Type=int,
1> Descendant_Class=Field::Integer
1> ]
错误C2248:'Field::Integer::Integer':无法访问在类'Field::Integer'中声明的受保护成员
c:\ProgramFiles\boost\boost\u 1\u 52\u 0\boost\operators.hpp(257):编译类模板成员函数“Field::Integer boost::operator+(Field::Integer,const Field::Integer&)”时
1> c:\ProgramFiles\boost\boost\u 1\u 52\u 0\boost\operators.hpp(836):请参阅正在编译的类模板实例化“boost::addable1”的参考
1> 与
1> [
1> T=字段::整数,
1> B=boost::detail::空基
1> ]
1> 请参阅正在编译的类模板实例化“boost::addable”的参考
1> 与
1> [
1> T=字段::整数
1> ]
1> 请参阅对正在编译的类模板实例化字段::Numeric的引用
1> 与
1> [
1> 值_Type=int,
1> 子体\类=字段::整数
1> ]
代码(简化为基本语句):
\ifndef字段\u整数\u水电站
#定义字段\整数\ HPP
#ifdef字段_导出
#定义字段\u API\u declspec(dllexport)
#否则
#定义字段API declspec(dllimport)
#恩迪夫
#包括“boost/operators.hpp”
名称空间字段
{
模板
类字段\ API数字
:public boost::可添加,
可减去的,
公共促进:可倍增,
可分割的
{
公众:
数值(常量值类型和新值=0);
数字(常量数字和fn);
虚拟~数值();
子体类运算符+=(常量子体类和dc);
子体类运算符-=(常量子体类和dc);
子体类运算符*=(常量子体类和dc);
子体类运算符/=(常量子体类和dc);
无效清除字段(无效);
bool支持作为字符串(void)常量的值;
};
类字段\ API整数
:公共字段::数字
{
公众:
//!析构函数
虚~整数();
受保护的:
//!构造函数
整数(const int new_值);
//!复制构造函数
整数(常量整数和fui);
};
}//结束命名空间字段
#endif//FIELD\u INTEGER\u水电站
我的目标是使上述代码成为可导出的调试DLL或发布DLL。代码生成时在静态库设置中没有错误 问题: 在上面的代码中,需要做哪些修改才能使其成为调试或发布DLL(Visual Studio 2008、Windows Vista、32位)
我搜索了web和StackOverflow,只得到了使用模板的结果,没有将类作为模板参数和DLL传递。发生错误的原因是
Numeric
继承自boost::addable
(以及另外3个相关类)。这将生成签名的非成员函数运算符+
Field::Integer boost::operator +(Field::Integer,const Field::Integer &)
它按值取左参数的原因是为了优化右值引用和复制省略。这需要访问boost::operator+
到Integer
的复制构造函数,该构造函数受保护,因此会出现错误。我不明白为什么编译为静态库对您有效,而DLL则不行
对于这样的访问问题,建议的方法是将复制构造函数公开。不想将Integer
作为叶类对我来说似乎是一个设计错误,因为如果这是您真正想要的,为什么您仍然希望能够将其用于加法和其他算术运算
另一种方法是将友谊授予boost::operator+(Integer,Integer const&)
。不推荐友谊路由,因为它依赖于boost::addable
的实现。通常,您会将友谊授予类boost::addable
,但它的实现将使用非成员朋友函数operator+
,而不是成员函数operator+
。您不应该让自己的类Integer
依赖于这些实现细节 发生错误的原因是Numeric
继承自boost::addable
(以及另外3个相关类)。这将生成签名的非成员函数运算符+
Field::Integer boost::operator +(Field::Integer,const Field::Integer &)
它按值取左参数的原因是为了优化右值引用和复制省略。这需要访问boost::operator+
到Integer
的复制构造函数,该构造函数受保护,因此会出现错误。我不明白为什么编译为静态库对您有效,而DLL则不行
对于这样的访问问题,建议的方法是将复制构造函数公开。不想将Integer
作为叶类对我来说似乎是一个设计错误,因为如果这是您真正想要的,为什么您仍然希望能够将其用于加法和其他算术运算
另一种方法是将友谊授予boost::operator+(Integer,Integer const&)
。不推荐友谊路由,因为它依赖于boost::addable
的实现。通常,您会将友谊授予类boost::addable
,但它的实现将使用非成员朋友函数operator+
,而不是成员函数operator+
。您不应该让自己的类Integer
依赖于这些实现细节 为什么字段::Integer
构造函数受保护