c++ - boost addable 和 dll 的辅助功能错误

标签 c++ templates visual-studio-2008 boost dll

我在 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

我的目标是将上述代码制作成可导出的Debug DLL或Release DLL。
代码构建时静态库设置没有错误。

问题:

在上面的代码中,需要做哪些修改才能使其成为 Debug 或 Release DLL(Visual Studio 2008、Windows Vista、32 位)?

我搜索了网络和 StackOverflow,我只得到了使用模板的结果,没有将类作为模板参数和 DLL 传递。

最佳答案

错误的发生是因为Numeric 继承自boost::addable(以及另外3 个相关的类)。这将生成一个非成员函数operator+ of signature

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 友元,但它的实现将使用非成员 friend 函数 operator+ 而不是成员函数 operator+ 。你不应该让你自己的类 Integer 依赖于这样的实现细节。

关于c++ - boost addable 和 dll 的辅助功能错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16779682/

相关文章:

c++ - HLSL 在代码中获取线程组数和线程数

c++ - 用于映射的 gluDisk 旋转

c++ - 在 C++ 中的类中调用函数?

python - CSS文件路径问题

visual-studio-2008 - 使用特定解决方案时 Visual Studio 崩溃

visual-studio - Visual Studio 2008 在打开 aspx/ascx 文件时挂起

c++ - ActiveX 属性(从 Visual Basic 访问)是否可以在运行时按程序生成?

c++ - 引用与模板模板类折叠

c++ - 通用回调

c++ - 空闲分配的内存生成异常 - C++