大神求指导:在其他的类中使用这个vector静态成员链接出错。
vector的定义
使用vector
编译通过了 连接错误
对eigen中的固定大小的类使用STL容器的时候,如果直接使用就会出错,比如
std::map<int, Eigen::Vector4f> std::vector<Eigen::Vector2d>
固定大小(fixed-size)的类是指在编译过程中就已经分配好内存空间的类,为了提高运算速度,对于SSE或者AltiVec指令集,向量化必须要求向量是以16字节即128bit对齐的方式分配内存空间,所以针对这个问题,容器需要使用eigen自己定义的内存分配器,即aligned_allocator。
使用它需要使用
#include <Eigen/StdVector>
对于上面的两个例子就变为
std::map<int, Eigen::Vector4f, Eigen::aligned_allocator<std::pair<const int, Eigen::Vector4f>> std::vector<Eigen::Vector2d,Eigen::aligned_allocator<Eigen::Vector2d>>
下面举一个例子
#include <Eigen\Dense> #include <vector> #include <Eigen/StdVector> using namespace std; struct mystruct { Eigen::Vector2d v2; Eigen::Matrix2f m2; }; int main() { //error //vector<mystruct> a; //right vector<mystruct,Eigen::aligned_allocator<mystruct>> a; mystruct b; for (int i = 0; i < 4; i++) { b.v2 = Eigen::Vector2d(1, 1); b.m2 = Eigen::Matrix2f::Identity(); a.push_back(b); } return 0; }
对于
使用
这
这是
这是
@mfc中在vector里添加控件类型的数据时出现C2248错误
这是我在.h文件中定义的2个容器
public: vector<CComboBox*> cbx; vector <CString> table1;
在对应cpp文件中初始化的方法
//注意以下6行在出错前我没有加上 而且下面cbx.push_back我放的都是控件类型 /*错误原因:MFC控件都继承自CObject类,这种类的对象不能被拷贝。 作为函数参数传递时,实际上是把控件变量拷贝了一份传递给函数了,导致编译错误。*/ //解决方法:用对应指针变量接收控件变量把控件变量放进容器中,所以可以看到.h中的容器类型已经改为 CComboBox* CComboBox* p_cbx_1=&cbx_1; CComboBox* p_cbx_2=&cbx_2; CComboBox* p_cbx_3=&cbx_3; CComboBox* p_cbx_4=&cbx_4; CComboBox* p_cbx_5=&cbx_5; CComboBox* p_cbx_6=&cbx_6; cbx.push_back(p_cbx_1); cbx.push_back(p_cbx_2); cbx.push_back(p_cbx_3); cbx.push_back(p_cbx_4); cbx.push_back(p_cbx_5); cbx.push_back(p_cbx_6); //table1不需要看 因为CString没有标题中的错误 table1.push_back(TEXT("1")); table1.push_back(TEXT("2")); table1.push_back(TEXT("3")); table1.push_back(TEXT("4")); table1.push_back(TEXT("5")); table1.push_back(TEXT("6")); //这里是用遍历把所有下拉框控件都添加同一组数据(即table1) for (int i=0;i<6;i++) { for (vector<CString>::iterator it=table1.begin();it!=table1.end();it++) { //这里的cbx[i]还是一个指针,所以要再加*取出地址对应变量,此时才是CComboBox变量 (*(cbx[i])).AddString(*it); } (*(cbx[i])).SetCurSel(i); }
第一次写csdn,多多包涵!
大神求指导:在其他的类中使用这个vector静态成员链接出错。
vector的定义
使用vector
编译通过了 连接错误
转载于:https://my.oschina.net/huohuli/blog/144319
症状:
在DLL中定义一个模板类并用__declspec(dllexport)定义。
1: template<typename T, int d>2: class DLLExportExportMacro vectortemp3: {
4: public:5:
6: vector m_Vector;
7:
8: public:9: vectortemp(vector v):m_Vector(d)
10: {
11: if (v.size() == d)12: {
13: m_Vector = v;
14: }
15: else16: throw invalid_argument("Vector size does not match.");17: }
18:
19: vectortemp(T v[]):m_Vector(d)
20: {
21: if (sizeof(v)/sizeof(T) == d)22: {
23: vector::iterator iter = m_Vector.begin();
24: int i = 0;25: while (iter != m_Vector.end())26: {
27: *iter++ = v[i];
28: }
29: }
30: else31: throw invalid_argument("Vector size does not match.");32:
33: }
34:
35: vectortemp():m_Vector(d)
36: {
37: vector::iterator iter = m_Vector.begin();
38: while (iter != m_Vector.end())39: {
40: *iter = 0;
41: }
42: }
43:
44: const T &operator[](int index) const45: {
46: return m_Vector[index];47: }
48:
49: T &operator[](int index)50: {
51: return m_Vector[index];52: }
53: };
警告提示出现在line 6。意思说vector需要用dll接口(__declspec(dllimport))从而使其被客户类vectortemp使用。__declspec(dllimport)用于所有需要从dll导入的对象,包括变量,函数,类等。而且必须用在定义前面。如果vectortemp中使用的不是一个模板类vector<>,这里会报错。因为头文件vector里只是vector类的声明。在vector实例化时,编译器会生成一份和头文件中一样的代码,一份本地类定义,当然加上模板参数,比如vector。所以用模板类不会出错,但编译器会友好地产生一个警告。警告不会影响程序运行,可以不用理睬。但是随着使用vectortemp的增加,警告会越来越多。这个确实很丑陋!解决方法有两个:直接用下面这个命令关闭警告。
1: #pragma warning( disable: 4251 )第一个方法还是丑陋了点,而且也不能了解内幕。我们需要一个显示地实例化模板,从而得到模板的一个定义。对一个非stl模板,可以这样:
1: template class DLLExportExportMacro SomeTemplate<int>;2: SomeTemplate<int> y;对于stl模板,如果只是相应的改成
1: template class DLLExportExportMacro std::vector<int>;2: std::vector<int> y;你会得到这个:
1: warning C4251: 'std::_Vector_val<_Ty,_Alloc>::_Alval' :2: class std::allocator<_Ty>' needs to have dll-interface to be used by clients of class 'std::_Vector_val<_Ty,_Alloc>'因为stl::vector的实际定义是"std::vector >"。所以还要import std::allocator。
1: template class DLLExportExportMacro std::allocator<int>;2: template class DLLExportExportMacro std::vector<int>;3: std::allocator<int> >;方法二:导致链接错误
如果同时有两个DLL库用用显示实例方法导出vector,会导致vector的重定义。唯一的解决办法就是用#pragma warning( disable: 4251屏蔽警告。