【发布时间】:2013-05-27 19:49:48
【问题描述】:
我在 Windows Vista 上使用 Visual Studio 2008,并将函数转换为调试 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) : 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> ]
代码(简化为基本语句):
#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。
代码在静态库设置中构建时没有错误。
问题:
在上述代码中,需要进行哪些修改才能将其转换为 Debug 或 Release DLL(Visual Studio 2008、Windows Vista、32 位)?
我搜索了网络和 StackOverflow,我只得到了使用模板的结果,没有将类作为模板参数和 DLL 传递。
【问题讨论】:
-
为什么
Field::Integer构造函数是protected?您可能希望将其更改为public或将friend class Numeric<int, Integer>添加到Integer类定义中。 -
构造函数是
protected,因为我不希望它作为叶类。 -
好吧,我不明白为什么它编译为静态库而不是 DLL,但友元类声明应该可以解决您的错误。
-
@rhalbersma:哪个班级应该成为朋友?
-
我制定了一个答案,我的友谊评论不正确。
标签: c++ templates visual-studio-2008 boost dll