我创建了这个matrix.cpp类,我重载了一些运算符:
template <typename T>
class MagicObject {
private:
std::vector<std::vector<T> > mat;
unsigned rows;
unsigned cols;
public:
MagicObject(unsigned _rows, unsigned _cols, const T& _initial);
MagicObject(const MagicObject<T>& rhs);
MagicObject();
~MagicObject();
// Operator overloading, for "standard" mathematical MagicObject operations
MagicObject<T>& operator=(const MagicObject<T>& rhs);
// MagicObject mathematical operations
MagicObject<T> operator+(const MagicObject<T>& rhs);
MagicObject<T>& operator+=(const MagicObject<T>& rhs);
MagicObject<T> operator-(const MagicObject<T>& rhs);
MagicObject<T>& operator-=(const MagicObject<T>& rhs);
MagicObject<T> operator*(const MagicObject<T>& rhs);
MagicObject<T>& operator*=(const MagicObject<T>& rhs);
MagicObject<T> transpose();
// matrix/scalar operations
MagicObject<T> operator+(const T& rhs);
MagicObject<T> operator-(const T& rhs);
MagicObject<T> operator*(const T& rhs);
MagicObject<T> operator/(const T& rhs);
// matrix/vector operations
std::vector<T> operator*(const std::vector<T>& rhs);
std::vector<T> diag_vec();
// Access the individual elements
T& operator()(const unsigned& row, const unsigned& col);
const T& operator()(const unsigned& row, const unsigned& col) const;
T& operator[](const unsigned &i);
// Access the row and column sizes
unsigned get_rows() const;
unsigned get_cols() const;
void _print();
};
我避免使用matrix.hpp,因为它很长。在任何情况下,标头包含正确工作的实现(当然在C ++中)。 现在我想使用SWIG创建一个python模块。为此,我创建了名为matrix.i的SWIG接口:
%module matrix
%{
#include "matrix.hpp"
%}
%include "matrix.hpp"
// used for the operator[]
%extend MagicObject<T> {
T& __getitem__(unsigned int i) {
return $self[i];
}
}
// used for _print() function
%extend MagicObject<T> {
void __print__() {
print $self;
}
}
%template(intMagicObject) MagicObject<int>;
%template(floatMagicObject) MagicObject<float>;
%template(doubleMagicObject) MagicObject<double>;
我使用这些命令来创建模块:
swig -c++ -python matrix.i
g++ -O2 -fPIC -c matrix.cpp
g++ -O2 -fPIC -c matrix_wrap.cxx -I/Users/dado/anaconda/include/python2.7
g++ -lpython -dynamclib matrix.o matrix_wrap.o -o _matrix.so
我执行最后一个命令时得到这些错误:
clang: warning: argument unused during compilation: '-dynamclib'
Undefined symbols for architecture x86_64:
"MagicObject<double>::MagicObject()", referenced from:
_wrap_new_doubleMagicObject(_object*, _object*) in matrix_wrap.o
_wrap_doubleMagicObject_transpose(_object*, _object*) in matrix_wrap.o
_wrap_doubleMagicObject___add__(_object*, _object*) in matrix_wrap.o
_wrap_doubleMagicObject___sub__(_object*, _object*) in matrix_wrap.o
_wrap_doubleMagicObject___div__(_object*, _object*) in matrix_wrap.o
_wrap_doubleMagicObject___mul__(_object*, _object*) in matrix_wrap.o
"MagicObject<float>::MagicObject()", referenced from:
_wrap_new_floatMagicObject(_object*, _object*) in matrix_wrap.o
_wrap_floatMagicObject_transpose(_object*, _object*) in matrix_wrap.o
_wrap_floatMagicObject___add__(_object*, _object*) in matrix_wrap.o
_wrap_floatMagicObject___sub__(_object*, _object*) in matrix_wrap.o
_wrap_floatMagicObject___div__(_object*, _object*) in matrix_wrap.o
_wrap_floatMagicObject___mul__(_object*, _object*) in matrix_wrap.o
"MagicObject<int>::MagicObject()", referenced from:
_wrap_new_intMagicObject(_object*, _object*) in matrix_wrap.o
_wrap_intMagicObject_transpose(_object*, _object*) in matrix_wrap.o
_wrap_intMagicObject___add__(_object*, _object*) in matrix_wrap.o
_wrap_intMagicObject___sub__(_object*, _object*) in matrix_wrap.o
_wrap_intMagicObject___div__(_object*, _object*) in matrix_wrap.o
_wrap_intMagicObject___mul__(_object*, _object*) in matrix_wrap.o
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我该如何解决这个问题?感谢
编辑这里是我的matrix.cpp文件(只是一个片段):
#include "matrix.hpp"
#include <iostream>
// Parameter Constructor
template<typename T>
MagicObject<T>::MagicObject(unsigned _rows, unsigned _cols, const T& _initial) {
mat.resize(_rows);
for (unsigned i=0; i<mat.size(); i++) {
mat[i].resize(_cols, _initial);
}
rows = _rows;
cols = _cols;
}
// Copy Constructor
template<typename T>
MagicObject<T>::MagicObject(const MagicObject<T>& rhs) {
mat = rhs.mat;
rows = rhs.get_rows();
cols = rhs.get_cols();
}
// (Virtual) Destructor
template<typename T>
MagicObject<T>::~MagicObject() {}
// Assignment Operator
template<typename T>
MagicObject<T>& MagicObject<T>::operator=(const MagicObject<T>& rhs) {
if (&rhs == this)
return *this;
unsigned new_rows = rhs.get_rows();
unsigned new_cols = rhs.get_cols();
// ..... rest here
答案 0 :(得分:0)
您需要在.cpp或.h文件中实例化模板类,就像
template class yourclass< double >;