我正在学习线性代数,并且认为我将一些新发现的知识转换为一个小型的C ++程序。对于这些数字,我偶然发现CLN, a Class Library for Numbers,我正在尝试使用它。
LinearEquation.h:
#include <iostream>
#include <cln/real.h>
#include <cln/rational.h>
using namespace std;
using namespace cln;
class LinearEquation{
public:
// constructor
LinearEquation(cl_R* coefficients, int numFactors, cl_R constant);
// copy constructor
LinearEquation(const LinearEquation& le);
// assignment constructor
LinearEquation& operator=(const LinearEquation& rhs);
// destructor
~LinearEquation();
/*
isSolution returns true if a provided set of rational numbers is a solution
to this linear Equation, and false otherwise.
No error checking is done, thus s and num is expected to match the number
of items in _coefficients;
*/
bool isSolution(const cl_R* s, int num);
private:
int _numFactors;
cl_R* _coefficients;
cl_R _constant;
};
LinearEquation.cpp:
#include "LinearEquation.h"
// constructor
LinearEquation::LinearEquation(cl_R* coefficients,
int numFactors,
cl_R constant){
_numFactors = numFactors;
_coefficients = new cl_R[numFactors];
for(int i=0; i<numFactors; i++){
_coefficients[i] = coefficients[i];
}
_constant = constant;
}
// copy constructor
LinearEquation::LinearEquation(const LinearEquation& obj){
_numFactors = obj._numFactors;
_coefficients = new cl_R[_numFactors];
for(int i=0; i<_numFactors; i++){
_coefficients[i] = obj._coefficients[i];
}
_constant = obj._constant;
}
// assignment constructor
LinearEquation& LinearEquation::operator=(const LinearEquation& le){
if(this != &le){
_numFactors = le._numFactors;
delete [] _coefficients;
_coefficients = new cl_R[_numFactors];
for(int i=0; i<_numFactors; i++){
_coefficients[i] = le._coefficients[i];
}
_constant = le._constant;
}
}
// destructor
LinearEquation::~LinearEquation(){
delete [] _coefficients;
}
bool LinearEquation::isSolution(const cl_R* s, int num){
cl_R sum = 0;
for(int i=0; i<_numFactors; i++){
sum = sum + (_coefficients[i] * s[i]);
}
return sum == _constant;
}
最后,main.cpp
#include <cln/integer.h>
#include <cln/real.h>
#include "LinearEquation.h"
#include <iostream>
using namespace cln;
using namespace std;
int main(){
int le1NumFactors = 2;
cl_R* le1Coefficients = new cl_R[le1NumFactors];
le1Coefficients[0] = 3;
le1Coefficients[1] = 2;
cl_R le1Constant = 7;
LinearEquation le1(le1Coefficients, le1NumFactors, le1Constant);
int le2NumFactors = 2;
cl_R* le2Coefficients = new cl_R[le2NumFactors];
le2Coefficients[0] = -1;
le2Coefficients[1] = 1;
cl_R le2Constant = 6;
LinearEquation le2(le2Coefficients, le2NumFactors, le2Constant);
cl_R* solution = new cl_R[le2NumFactors];
solution[0] = -1;
solution[1] = 5;
cout << "(-1, 5) is " << (le1.isSolution(solution, 2) ? "" : "not ")
<< "a solution to le1." << endl;
cout << "(-1, 5) is " << (le2.isSolution(solution, 2) ? "" : "not ")
<< "a solution to le2." << endl;
delete [] le1Coefficients;
delete [] le2Coefficients;
delete [] solution;
return 0;
}
g ++ LinearEquation.h LinearEquation.cpp -c
工作正常,但
g ++ main.cpp LinearEquation.o
没有。生成以下错误消息:
lowerkey @ cassiopeia:〜/ Desktop / math $ g ++ main.cpp LinearEquation.o /tmp/ccMceM9l.o:在功能上
__static_initialization_and_destruction_0(int, int)': main.cpp:(.text+0x4ab): undefined reference to
CLN :: cl_random_def_init_helper :: cl_random_def_init_helper()” main.cpp :(。text + 0x4b0):未定义引用cln::cl_random_def_init_helper::~cl_random_def_init_helper()' main.cpp:(.text+0x4e0): undefined reference to
CLN :: cl_FF_globals_init_helper :: cl_FF_globals_init_helper()” main.cpp :(。text + 0x4e5):未定义的引用cln::cl_FF_globals_init_helper::~cl_FF_globals_init_helper()' main.cpp:(.text+0x509): undefined reference to
CLN :: cl_DF_globals_init_helper :: cl_DF_globals_init_helper()” main.cpp :(。text + 0x50e):未定义引用cln::cl_DF_globals_init_helper::~cl_DF_globals_init_helper()' main.cpp:(.text+0x532): undefined reference to
CLN :: cl_LF_globals_init_helper :: cl_LF_globals_init_helper()” main.cpp :(。text + 0x537):未定义引用cln::cl_LF_globals_init_helper::~cl_LF_globals_init_helper()' /tmp/ccMceM9l.o: In function
CLN :: cl_gc_dec_pointer_refcount(CLN :: cl_heap *)': main.cpp中:( text._ZN3cln26cl_gc_dec_pointer_refcountEPNS_7cl_heapE [CLN :: cl_gc_dec_pointer_refcount(CLN :: cl_heap *)] + 0×28): 对cln::cl_free_heap_object(cln::cl_heap*)' /tmp/ccMceM9l.o: In function
cln :: cl_I_classes_dummy :: cl_I_classes_dummy()'的未定义引用: 。main.cpp中:( text._ZN3cln18cl_I_classes_dummyC1Ev [CLN :: cl_I_classes_dummy :: cl_I_classes_dummy()] + 0x9): 未定义引用cln::cl_class_fixnum' LinearEquation.o: In function
LinearEquation :: isSolution(cln :: cl_R const *,int)': LinearEquation.cpp :(。text + 0x61a):未定义的引用cln::operator*(cln::cl_R const&, cln::cl_R const&)' LinearEquation.cpp:(.text+0x636): undefined reference to
cln :: operator +(cln :: cl_R const&amp;,cln :: cl_R const&amp;)'LinearEquation.o: 在函数__static_initialization_and_destruction_0(int, int)': LinearEquation.cpp:(.text+0x741): undefined reference to
中cln :: cl_random_def_init_helper :: cl_random_def_init_helper()' LinearEquation.cpp :(。text + 0x746):未定义的引用cln::cl_random_def_init_helper::~cl_random_def_init_helper()' LinearEquation.cpp:(.text+0x76a): undefined reference to
CLN :: cl_FF_globals_init_helper :: cl_FF_globals_init_helper()” LinearEquation.cpp :(。text + 0x76f):未定义的引用cln::cl_FF_globals_init_helper::~cl_FF_globals_init_helper()' LinearEquation.cpp:(.text+0x793): undefined reference to
CLN :: cl_DF_globals_init_helper :: cl_DF_globals_init_helper()” LinearEquation.cpp :(。text + 0x798):未定义的引用cln::cl_DF_globals_init_helper::~cl_DF_globals_init_helper()' LinearEquation.cpp:(.text+0x7bc): undefined reference to
CLN :: cl_LF_globals_init_helper :: cl_LF_globals_init_helper()” LinearEquation.cpp :(。text + 0x7c1):未定义的引用cln::cl_LF_globals_init_helper::~cl_LF_globals_init_helper()' LinearEquation.o: In function
cln :: operator ==(cln :: cl_R const&amp ;,, cln :: cl_R const&amp;)': LinearEquation.cpp :(。text._ZN3clneqERKNS_4cl_RES2_ [CLN :: ==操作符(CLN :: cl_R const&amp;,cln :: cl_R const&amp;)] + 0x14):未定义的引用 `cln :: equal(cln :: cl_R const&amp;,cln :: cl_R const&amp;)'collect2:ld返回 1退出状态
答案 0 :(得分:4)
看起来你没有链接cln库。尝试以下(假设库已正确安装):
g++ main.cpp LinearEquation.o -lcln
有关编译的更多详细信息,请参阅文档: http://www.ginac.de/CLN/cln_11.html#SEC64