【发布时间】:2012-11-06 13:59:57
【问题描述】:
考虑一个简单的类,它只包含内联的成员函数。例如:
template <typename T1, typename T2>
class Point2D {
public:
typedef Point2D<T1,T2> ThisType;
typedef T1 Tx;
typedef T2 Ty;
T1 x;
T2 y;
inline Point2D() : x(0), y(0) {}
inline Point2D(T1 nx, T2 ny) : x(nx), y(ny) {}
inline Point2D(const Point2D& b) : x(b.x), y(b.y) {}
inline Point2D& operator=(const Point2D& b) { x=b.x; y=b.y; return *this; }
inline ~Point2D() {}
};
typedef Point2D<int,int> Int2;
当我想导出到 DLL 的另一个类(例如,MyClass,成员 Int2 point)中使用类型为 Int2 的对象时,我收到以下警告:
警告 C4251:“MyClass::point”:“Point2D”类需要有 dll 接口才能供“MyClass”类的客户端使用
但是,如果我按照警告提示将__declspec(dllexport) 放入“Point2D”的定义中(这对我来说似乎很愚蠢,因为所有函数都是内联的,而且它是一个模板,see SO question),我会得到以下信息尝试在另一个项目中使用 DLL 时出错:
错误 LNK2019:无法解析的外部符号“__declspec(dllimport) public: __thiscall lwin::Point2D::Point2D(int,int)” ...
注意,Point2D 的定义在所有项目可见的标题中给出。
我该怎么办?跳过dllexport 并忽略警告?还是有一些巧妙的技巧可以避免这种编译器混淆?
【问题讨论】:
-
您收到警告是因为您忘记了编译器自动生成的复制构造函数和赋值运算符。只需将它们添加到类或忽略警告。
-
好收获!固定的。但问题依然存在……
标签: c++ visual-c++ dll dllexport