使用SWIG编译时重载运算符C++错误
overload operators C++ errors when compiling with SWIG
我创建了这个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)
我该如何解决此问题?感谢
EDIT这里是我的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
您需要在.cpp或.h文件中实例化模板类,就像一样
template class yourclass< double >;
相关文章:
- 为什么比较运算符如此快速
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 增量运算符与后缀混淆
- 一个关于在C++中重载布尔运算符的问题
- 运算符C++ "delete []"仅删除 2 个前值
- 模板类无法识别友元运算符
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 关闭||运算符优化
- 通过继承类使用来自不同命名空间的运算符
- C++Cast运算符过载
- 如何使用AngelScript注册SFML Vector2运算符
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 布尔比较运算符是如何在C++中工作的
- 重载运算符new[]的行为取决于析构函数
- 如何防止clang格式在流运算符调用之间添加换行符<<