boost addable和dll的可访问性错误
Accessibility error with boost addable and dll
我正在使用Visual Studio 2008,在Windows Vista上,并将函数转换为构建调试DLL。
编译器错误:
我得到一个可访问性错误与boost::操作符模板:
error C2248: 'Field::Integer::Integer' : cannot access protected member declared in class 'Field::Integer'
c:program filesboostboost_1_52_0boostoperators.hpp(257) : while compiling class template member function 'Field::Integer boost::operator +(Field::Integer,const Field::Integer &)'
1> c:program filesboostboost_1_52_0boostoperators.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> ]
代码(简化为基本语句):
#ifndef FIELD_INTEGER_HPP
#define FIELD_INTEGER_HPP
#ifdef FIELD_EXPORTS
#define FIELD_API __declspec(dllexport)
#else
#define FIELD_API __declspec(dllimport)
#endif
#include "boost/operators.hpp"
namespace Field
{
template <class Value_Type, class FIELD_API Descendant_Class>
class FIELD_API Numeric
: public boost::addable<Descendant_Class>,
public boost::subtractable<Descendant_Class>,
public boost::multipliable<Descendant_Class>,
public boost::dividable<Descendant_Class>
{
public:
Numeric(const Value_Type& new_value = 0);
Numeric(const Numeric& fn);
virtual ~Numeric();
Descendant_Class operator+=(const Descendant_Class& dc);
Descendant_Class operator-=(const Descendant_Class& dc);
Descendant_Class operator*=(const Descendant_Class& dc);
Descendant_Class operator/=(const Descendant_Class& dc);
void clear_field(void);
bool supports_value_as_string(void) const;
};
class FIELD_API Integer
: public Field::Numeric<int, Field::Integer>
{
public:
//! Destructor
virtual ~Integer();
protected:
//! Constructor
Integer(const int new_value);
//! Copy constructor
Integer(const Integer& fui);
};
} // End namespace Field
#endif // FIELD_INTEGER_HPP
我的目标是把上面的代码变成一个可导出的调试DLL或发布DLL。
生成的代码在静态库设置中没有错误。
问题:
在上面的代码中,什么是必要的修改,使其成为调试或发布DLL (Visual Studio 2008, Windows Vista, 32位)?
我搜索了web和StackOverflow,我只得到使用模板的结果,而不是传递类作为模板参数和dll。
由于Numeric
继承了boost::addable
(以及3个相关的类)而发生错误。这将生成一个签名为
operator+
Field::Integer boost::operator +(Field::Integer,const Field::Integer &)
按值接受左实参的原因是为了优化右值引用和复制省略。这需要boost::operator+
访问Integer
的复制构造函数,即protected
,因此您会得到一个错误。我不明白为什么作为静态库编译对你有用,而DLL却不行。
对于这样的访问问题,建议使用复制构造函数public
。对我来说,不希望Integer
作为叶子类似乎是一个设计错误,因为如果这真的是你想要的,为什么你仍然希望能够使用它进行加法和其他算术运算?
另一种选择是授予boost::operator+(Integer, Integer const&)
友谊。不推荐使用友谊路由,因为它依赖于boost::addable
的实现。通常情况下,您会将友谊授予类boost::addable
,但它的实现将使用非成员朋友函数operator+
而不是成员函数operator+
。您不应该让自己的类Integer
依赖于这样的实现细节。
相关文章:
- 为什么我的 setter 方法会产生错误的访问错误
- OpenCV C++ 3 维垫数据访问错误值
- C++ 将元素分配给映射值时访问错误
- 为什么代码会抛出非法内存访问错误
- 访问字符串::c_str()的结果时出现瓦尔格林德内存访问错误
- 从触摸回调访问向量元素的方法时,不良访问错误
- 筛子上的访问错误
- 查找() 上的 C++ 错误访问错误
- OPENCV MAT内存访问错误
- C - 空数对象的初始化发送不良访问错误
- std::函数 创建临时内存时内存访问错误
- 不良访问C 错误
- C 中的模棱两可的访问错误
- Win 32写入文件:访问错误1784
- 奇怪的结构访问错误
- 64位Windows API结构对齐导致命名管道上出现“拒绝访问”错误
- 不是NULL指针,而是错误的访问错误消息
- 统一读取和不可修复的访问错误
- 带有 libcurl C++的内存访问错误
- 非法内存访问错误