![c++与matcom混合编程 c++与matcom混合编程]()
![c++与matcom混合编程 c++与matcom混合编程]()
![c++与matcom混合编程 c++与matcom混合编程]()
#include
#include 
#include
"matlib.h"
using namespace
std;
int main()
{
     
initM(MATCOM_VERSION);     
//初始化Matcom C++矩阵库,当与exitM()同时使用,否则会造成内存泄露
     
Mm
a;     
//创建矩阵a
     
Mm
b;     
//创建矩阵b
     
Mm
x;     
//创建矩阵x
     
a = rand(3,
3);     
//随机产生一个3×3的矩阵
     
b = zeros(3, 3);     
//生成一个全零矩阵,并赋值给b
     
b.r(1, 1) =
1;     
//矩阵b的第一行第一列赋值为1
     
b.r(2, 2) =
3;     
//矩阵b的第二行第二列赋值为3
     
b.r(3, 3) =
5;     
//矩阵b的第三行第三列赋值为5
     
cout
<< "a=:"
<< endl;
     
display(a);     
//输出矩阵a
     
cout
<< "b=:"
<< endl;
     
display(b);     
//输出矩阵b
     
x = a + b;
     
cout
<<
"a+b=:\n";
     
display(x);
     
cout
<<
"矩阵a的行列式值为:" << endl;
     
display(det(a));     
//输出矩阵a的行列式
     
cout
<<
"矩阵b的行列式为:" << endl;
     
display(det(b));     
//输出矩阵a的行列式
     
cout
<<
"x矩阵的遍历:" << endl;
     
for (int i = 1; i <= x.rows();i++)
     
{
     
     
for (int j = 1; j <= x.cols();j++)
     
     
{
     
     
     
cout
<<
setiosflags(ios::fixed)
<< setprecision(2)
<< setw(6)
<< x.r(i, j);
     
     
}
     
     
cout
<<
"\n";
     
}
     
exitM();
     
return 0;
}
直接在vs当中运行此代码不能通过编译会报错,错误如下:
1>------ 已启动生成: 
项目: matcom, 配置: Debug Win32 ------
1> 
源.cpp
1>d:\matcom45\lib\matlib.h(2867):
error C2375: “abs”: 重定义;不同的链接
1>         
d:\program files (x86)\microsoft visual studio
12.0\vc\include\math.h(1025) : 参见“abs”的声明
========== 生成: 
成功
0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
![c++与matcom混合编程 c++与matcom混合编程]()
通过错误我们知道是函数abs发生了重定义,定位到matcom的matlib.h头文件当中
#if !defined(_MSC_VER) |
(_MSC_VER>=1100)
m_type DLLI abs(m_type
x);
#endif
![c++与matcom混合编程 c++与matcom混合编程]()
注释掉该定义
#if !defined(_MSC_VER) |
(_MSC_VER>=1100)
//m_type DLLI abs(m_type
x);
#endif
![c++与matcom混合编程 c++与matcom混合编程]()
返回vs重新编译程序,发现编译能够成功,运行结果:
![c++与matcom混合编程 c++与matcom混合编程]()