2012-02-09 8 views
1

私は線形代数を学んでいます。新しい発見された知識の一部を小さな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; 
} 

グラム++ LinearEquation.h LinearEquation.cpp -c

が正常に動作しますが、

グラム++ main.cppにLinearEquation.o

はしていません。次のエラーメッセージが生成されます

lowerkeyする@カシオペア:〜/デスクトップ/数学$ 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に:(テキスト+ 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に:(テキスト+ 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 :(。テキスト+ 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に:(テキスト+ 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に:(テキスト。 _ZN3cln26cl_gc_dec_pointer_refcountEPNS_7cl_heapE [CLN :: cl_gc_dec_pointer_refcount(CLN :: cl_heap *)] +の0x28):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): 未定義参照LinearEquation cln::cl_class_fixnum' LinearEquation.o: In functionへ:: isSolution(CLN :: CL_Rのconst *、int型) ': LinearEquation.cpp :(テキスト+ 0x61a):。 cln::operator*(cln::cl_R const&, cln::cl_R const&)' LinearEquation.cpp:(.text+0x636): undefined reference to CLNへの未定義参照::演算子+(cln :: cl_R const &、CLN :: CL_RのCONST &) '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 :(テキスト+ 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 :(テキスト+ 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()に未定義の参照' cln::cl_DF_globals_init_helper::~cl_DF_globals_init_helper()' LinearEquation.cpp:(.text+0x7bc): undefined reference to CLNに未定義参照。 LinearEquation.cpp :(テキスト+ 0x798) :: cl_LF_globals_init_helper :: cl_LF_globals_init_helper() ' LinearEquation.cpp :(。テキスト+ 0x7c1): cln::cl_LF_globals_init_helper::~cl_LF_globals_init_helper()' LinearEquation.o: In function CLNに未定義の参照::演算子==(CLN :: CL_RのCONST &、 CLN :: CL_RのCONST &) ': LinearEquation.cpp :(text._ZN3clneqERKNS_4cl_RES2_ [CLN ::演算子= =(CLN :: CL_R CONST &、CLN :: CL_RのCONST &)] + 0x14に): `CLNに未定義の参照::等しい(CLN :: CL_RのCONST &、CLN :: CL_RのCONST &)」collect2は:LD 終了ステータス

+0

はCLN –

+0

ためのライブラリをリンクします。最近ではトロールがたくさんあります。質問にはすべての可能な情報が含まれており、よく書かれています。 – Vangel

答えて

3

clnライブラリをリンクしていないようです。 (ライブラリが正しくインストールされていると仮定して)以下のことを試してみてください。

g++ main.cpp LinearEquation.o -lcln 

は、コンパイルの詳細については、マニュアルを参照してください:この質問は、downvotedされている理由 http://www.ginac.de/CLN/cln_11.html#SEC64

関連する問題