einfache c-programme wollen nicht



  • Moinsen zusammen,

    ich habe mal versucht ein wenig an den Basics zu feilen und wollte mir eine einfache Klasse für komplexe Zahlen schreiben. Als Grundlage diente hierfür das nicht ganz unbekannte Büchlein von Bjarne Stroustoup.

    Hier erstmal der Code (nicht ganz von mir, aber da es den gleichen Fehler aufweist wie meine Variante und sich direkt am Code vom Buch orientiert, poste ich es einfach mal.

    //////////////////////////////////////////////////////////
    //
    // complex.h - Header file of complex number class
    //
    //////////////////////////////////////////////////////////
    
    #ifndef COMPLEX_H_
    #define COMPLEX_H_
    
    #include <iostream.h>
    
    class Complex
    {   float real;
        float imag;
       public:
        Complex(float r = 0, float i = 0)   //default, 1, & 2 arg constructor!
        {   real = r;
            imag = i;
        }                               //note the absence of ;s on inline code!
        ~Complex() {}
        void add(const Complex& c);     //member functions.
        void sub(const Complex& c);
        const double abs() const;
        // overloaded operators for iostream ops.
        friend istream& operator>>(istream& is, Complex& c);
        friend ostream& operator<<(ostream& os, const Complex& c);
        // overloaded operators for arithmetic ops.
        friend const Complex operator+(const Complex& left, const Complex& right);
        friend const Complex operator-(const Complex& left, const Complex& right);
        friend const Complex operator*(const Complex& left, const Complex& right);
        friend const Complex operator/(const Complex& left, const Complex& right);
        // overloaded operators for logical ops.
        friend const bool operator==(const Complex& left, const Complex& right);
        friend const bool operator!=(const Complex& left, const Complex& right);
        friend const bool operator<=(const Complex& left, const Complex& right);
        friend const bool operator>=(const Complex& left, const Complex& right);
        friend const bool operator<(const Complex& left, const Complex& right);
        friend const bool operator>(const Complex& left, const Complex& right);
        // overloaded assignment operators
        friend const Complex& operator+=(Complex& left, const Complex& right);
        friend const Complex& operator-=(Complex& left, const Complex& right);
        friend const Complex& operator*=(Complex& left, const Complex& right);
        friend const Complex& operator/=(Complex& left, const Complex& right);
    };
    
    #endif // COMPLEX_H_
    
    //////////////////////////////////////////////////////////
    //
    // complex.cpp - Implementation of complex number class
    //
    //////////////////////////////////////////////////////////
    
    #include <iostream.h>
    #include <assert.h>
    #include <math.h>             // sqrt()
    #include "complex.h"
    
    void Complex::add(const Complex& c)             //non-inlined member fns.
    {
        real += c.real;
        imag += c.imag;
    }
    
    void Complex::sub(const Complex& c)
    {
        real -= c.real;
        imag -= c.imag;
    }
    
    const double Complex::abs() const
    {
        return sqrt(real * real + imag * imag);
    }
    
    // Global operators overloaded for class Complex:
    
    istream& operator>>(istream& is, Complex& c)
    {
        return is >> c.real >> c.imag;
    }
    
    ostream& operator<<(ostream& os, const Complex& c)
    {
        os << "(" << c.real << (c.imag < 0 ? "" : "+") << c.imag << "i" << ")";
        return os;
    }
    
    // Global arithmetic operators overloaded on class Complex
    
    const Complex operator+(const Complex& left, const Complex& right)
    {
        return Complex(left.real + right.real, left.imag + right.imag);
    }
    
    const Complex operator-(const Complex& left, const Complex& right)
    {
        return Complex(left.real - right.real, left.imag - right.imag);
    }
    
    const Complex operator*(const Complex& left, const Complex& right)
    {
        return Complex(left.real * right.real - left.imag * right.imag,
                            left.real * right.imag + left.imag * right.real);
    }
    
    const Complex operator/(const Complex& left, const Complex& right)
    {
        float num1, num2, denom;
    
        num1 = left.real * right.real + left.imag * right.imag;
        num2 = left.imag * right.real - left.real * right.imag;
        denom = right.real * right.real + right.imag * right.imag;
        assert(denom);
    
        return Complex(num1/denom, num2/denom);
    }
    
    // Global logical operators overloaded on class Complex
    
    const bool operator==(const Complex& left, const Complex& right)
    {
        return (left.real == right.real) && (left.imag == right.imag);
    }
    
    const bool operator!=(const Complex& left, const Complex& right)
    {
        return (left.real != right.real) || (left.imag != right.imag);
    }
    
    const bool operator<=(const Complex& left, const Complex& right)
    {
        return left.abs() <= right.abs();
    }
    
    const bool operator>=(const Complex& left, const Complex& right)
    {
        return left.abs() >= right.abs();
    }
    
    const bool operator<(const Complex& left, const Complex& right)
    {
        return left.abs() < right.abs();
    }
    
    const bool operator>(const Complex& left, const Complex& right)
    {
        return left.abs() > right.abs();
    }
    
    // Global mathematical assignment operators overloaded on class Complex
    
    const Complex& operator+=(Complex& left, const Complex& right)
    {
        left.real += right.real;
        left.imag += right.imag;
        // also could just do: left = left + right;
        return left;
    }
    
    const Complex& operator-=(Complex& left, const Complex& right)
    {
        left.real -= right.real;
        left.imag -= right.imag;
        // also could just do: left = left - right;
        return left;
    }
    
    const Complex& operator*=(Complex& left, const Complex& right)
    {
        left = left * right;
        return left;
    }
    
    const Complex& operator/=(Complex& left, const Complex& right)
    {
        left = left / right;
        return left;
    }
    
    //////////////////////////////////////////////////////////
    //
    // cmplxtst.cpp - Test of complex number class
    //
    //////////////////////////////////////////////////////////
    
    #include <iostream.h>
    #include "complex.h"
    
    main() {
    
        cout << "Test of complex numbers." << endl << endl;
    
        cout << "Constructor calls:" << endl;
        cout << "   Complex()    = " << Complex() << endl;
        cout << "   Complex(1)   = " << Complex(1) << endl;
        cout << "   Complex(1,2) = " << Complex(1,2) << endl << endl;
    
        Complex a(1.0,2.0);                 //floats.
        Complex b(3,4);                     //ints.
        Complex c(5);
    
        cout << "   a = " << a << endl;
        cout << "   b = " << b << endl;
        cout << "   c = " << c << endl;
    
        Complex d = a;                          //force copy constructor call.
    
        cout << endl << "Test copy c'tor by making d a copy of a:" << endl;
        cout << "   d = " << d << endl << endl;
    
        cout << "Simple arithmetic operations:" << endl;
        cout << "   a + b = " << a + b << endl;
        cout << "   a - b = " << a - b << endl;
        cout << "   a * b = " << a * b << endl;
        cout << "   a / b = " << a / b << endl << endl;
    
        cout << "Using automatic type conversion:" << endl;
        cout << "   d + 1 = " << d + 1 << endl;
        cout << "   (a + b) / (c + 1) = " << (a + b) / (c + 1) << endl << endl;
    
        cout << "Using the add() and sub() member functions:" << endl;
        a.add(b); cout << "   a + b = " << a << endl;
        a.sub(b); cout << "   a + b - b = " << a << endl;
        a.sub(b); cout << "   a + b - b - b = " << a << endl << endl;
    
        cout << "A complex number identity result:" << endl
              << "   (a+b)*(a-b) / ((a-b)*(a+b)) = "
              <<     (a+b)*(a-b) / ((a-b)*(a+b)) << endl << endl;
    
        cout << "And another:" << endl
              << "   (a+b)*(a-b) / ((b-a)*(b+a)) = "
              <<     (a+b)*(a-b) / ((b-a)*(b+a)) << endl;
    
        return 0;
    }
    

    Soweit so gut - beim Compilieren des gesamten Projektes mit BCB6 entstehen merwürdige Linker-Errors wie diese:

    [Linker Error] Unresolved external 'Complex::add(const Complex&)' referenced from C:\CPLX\CMPLXTST.OBJ
    [Linker Error] Unresolved external 'Complex::sub(const Complex&)' referenced from C:\CPLX\CMPLXTST.OBJ
    [Linker Error] Unresolved external 'operator -(const Complex&, const Complex&)' referenced from C:\CPLX\CMPLXTST.OBJ
    [Linker Error] Unresolved external 'operator +(const Complex&, const Complex&)' referenced from C:\CPLX\CMPLXTST.OBJ
    [Linker Error] Unresolved external 'operator *(const Complex&, const Complex&)' referenced from C:\CPLX\CMPLXTST.OBJ
    [Linker Error] Unresolved external 'operator /(const Complex&, const Complex&)' referenced from C:\CPLX\CMPLXTST.OBJ
    [Linker Error] Unresolved external 'operator <<(_STL::basic_ostream<char, _STL::char_traits<char> >&, const Complex&)' referenced from C:\CPLX\CMPLXTST.OBJ

    Weis hier einer einen Rat? Hat es was mit dem Compiler zu tun, oder muss man für solch "primitiven" Code irgendwas an den Optionen drehen?

    Danke Schonmal!

    Beechen

    [ Dieser Beitrag wurde am 30.03.2003 um 18:25 Uhr von Beechen editiert. ]



  • Haste die 'Unit' Complex dem Projekt auch zugeordnet? (Der scheint die Complex.obj nicht zu finden?)

    Ist die .obj in den Suchpfaden zu finden?



  • 1. Fehler: Ist kein C, sondern C++.
    2. Fehler: Ich vermute, du hast die complex.cpp nicht zum Projekt hinzugefügt (Projekt->Dem Projekt hinzufügen).



  • oki, danke erstmal euch beiden, nachdem ich es dem Projekt hinzugefügt habe, funzte es auch wie gewünscht.

    Eine Frage bleibt allerdings noch offen -- was ist, wenn ich nur die drei oben angegebenen Dateien mittels bcb32.exe compilieren möchte? Da funktioniert es nähmlich immer noch nicht. Liegt es an den Suchpfaden für die LIBS?

    Danke nochmal

    Beechen



  • K.A.

    schau mal in die Hilfe zu

    bcc32 und
    ilink32


Anmelden zum Antworten