写了个类模板,想导出来做成dll形式,不知道技术上可行不,光是导出类还是可以的,但是导出模板类就不行了,在网上没有找到关可以的方法,在此请教各位高手。现以一个简单的模板类做试验:
头文件:
#ifndef POINT_H
#define  POINT_H
 
template<class ET>
class Point
 {
 public:
 ET x;
 ET y;
 Point();
 Point(ET x_coordinate,ET y_coordinate);
 void Print();
 };
#endif源文件:
#include "iostream.h"
#include "Dlltest.h"
template<class ET>
Point::Point()
{
x=0;
y=0;
}
template<class ET>
Point::Point(ET x_coordinate,ET y_coordinate)
{
x=x_coordinate;
y=y_coordinate;
}
template<class ET>
void Point::Print()
{
cout<<"x="<<x<<"y="<<y<<endl;
}用如下最简单的方式可以导出,(源文件不要template)
#ifndef DLLTEST_H
#define DLLTEST_Hextern "C" class _declspec(dllexport) Point
 {
 public:
 float x;
 float y;
 Point();
 Point(float x_coordinate,float y_coordinate);
 void Print();
 };
#endif
但是如果要把template一起导出呢,请问怎么办?
再问一下,当加了extern "C"修饰之后,导出的dll还是发生了名字改编,应该怎样消除?谢谢!

解决方案 »

  1.   

    好像是,我看了一下,像vector这样的STL容器,它们都是把定义和实现放在一个头文件中,然后放在VC的include目录下的,
      

  2.   

    vc不支持分开写,有一些编译器支持,比如c++ builder
      

  3.   

    vc不支持分开写,有一些编译器支持,比如c++ builder
    -------------------------
    c++ builder 不支持模板类的分离编译模板的本质是编译器的源码级的展开
    既在 参数化的时候 是必须要cpp代码的所以用DLL导出化 你只能实例化后导出
    或者将 .h 和 .cpp放一个文件 做成*.obj