It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
已关闭8年。
我正在学习线性代数,并认为我会将一些新发现的知识转换成一个小的C++程序。对于这些数字,我偶然发现了我尝试使用的CLN, a Class Library for Numbers。
LinearEquation.h:
LinearEquation.cpp:
最后是main.cpp
工作正常,但是
没有。产生以下错误消息:
请参阅文档以获取有关编译的更多详细信息:
http://www.ginac.de/CLN/cln_11.html#SEC64
已关闭8年。
我正在学习线性代数,并认为我会将一些新发现的知识转换成一个小的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;
}
工作正常,但是
没有。产生以下错误消息:
最佳答案
看起来您没有链接cln库。请尝试以下操作(假设库已正确安装):
g++ main.cpp LinearEquation.o -lcln
请参阅文档以获取有关编译的更多详细信息:
http://www.ginac.de/CLN/cln_11.html#SEC64
10-08 09:36