Hackerboard Wiki HaboBlog
Hackerboard bei Facebook Hackerboard bei Google+ Hackerboard bei Twitter

[HaBo]

 
Code Kitchen Allgemeines Coder-Forum rund um das Programmieren eigenständiger, ausführbarer Programme.

C++ Klassenhierachie Fehler beim Kompilieren

Diskussion: C++ Klassenhierachie Fehler beim Kompilieren im Forum Code Kitchen, in der Kategorie Software Home; Anzeige Bin derzeit dabei mir C++ beizubringen. Bei einer Aufgabe zum Thema Vererbung, soll ich 2 Klassen(GiroKonto, SparKonto) von Konto ...

Antwort
Alt 15.07.11, 17:41   #1 (permalink)
 
Registriert seit: 09.07.11
Snow! Leistung: Facit NTK
Likes: 0
Standard C++ Klassenhierachie Fehler beim Kompilieren

Anzeige

Bin derzeit dabei mir C++ beizubringen. Bei einer Aufgabe zum Thema Vererbung, soll ich 2 Klassen(GiroKonto, SparKonto) von Konto ableiten, und paar kleine Werte/Funktionen hinzufügen.

CODE Achtung lang   

Code:
/*
 * Konto.h
 *
 *  Created on: 08.07.2011
 *      Author: snow
 */

#ifndef KONTO_H_
#define KONTO_H_
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

class Konto{

private:
    string k_name;
    unsigned long k_nr;
    double k_stand;

public:
    Konto(const string& name = "Unbekannt", unsigned long nr = 1111111L, double stand = 0.0){
        k_name =  name;
        k_nr = nr;
        k_stand = stand;
    }
    virtual ~Konto(){}

    const string& getName() const {return k_name;}
    void setName(const string& s){ k_name = s;}

    long getNr() const {return k_nr;}
    void setNr(long nr){k_nr = nr;}

    double getStand() const {return k_stand;}
    void setStand(double s){k_stand = s;}


    void display() const;

    ostream& write(ostream& os) const;
    istream& read(istream& is);

};


#endif

/*
 * Konto.cpp
 *
 *  Created on: 08.07.2011
 *      Author: snow
 */
#include "Konto.h"

using namespace std;





void Konto::display() const{
    cout<< "Inhaber: " << k_name << endl
        << "Kontonummer: " << k_nr << endl
        << "Stand: "<< k_stand << endl;

}


ostream& Konto::write(ostream& os) const{
    os << name << '\0';
    os.write((char*)&nr,sizeof(nr));
    os.write((char*)&stand, sizeof(stand));
    return os;
}

istream& Konto::read(istream& is){
    getline(is, name, '\0');
    is.read( (char*)&nr, sizeof(nr));
    is.read( (char*)&stand, sizeof(stand));
    return is;
}

/*
 * GiroKonto.h
 *
 *  Created on: 15.07.2011
 *      Author: snow
 */

#ifndef GIROKONTO_H_
#define GIROKONTO_H_

#include "Konto.h"

class GiroKonto : public Konto {

private:
    double limit;
    float sollzins;

public:
    GiroKonto(double l = 1000.00, float sz = 00.4F, const string& name = "Unbekannt", unsigned long nr = 1111111L, double stand = 0.0);
    ~GiroKonto(){}

    void setLimit(double l){ limit = l;}
    double getLimit() const{ return limit;}

    void setZins(float sz){ sollzins = sz;}
    float getZins() const{ return sollzins;}

    void display() const;

};

#endif /* GIROKONTO_H_ */

/*
 * GiroKonto.cpp
 *
 *  Created on: 15.07.2011
 *      Author: snow
 */

#include "GiroKonto.h"
#include "Konto.h"
using namespace std;

GiroKonto::GiroKonto(double l, float sz, const string& name, unsigned long nr, double stand)
    : Konto(name, nr, stand), limit(l), sollzins(sz) {
    ;

}

void GiroKonto::display() const{
    Konto::display();

    cout << "Überziehungslimit: " << limit << endl;
    cout << "Sollzins: " << sollzins << endl;


}

/*
 * SparKonto.cpp
 *
 *  Created on: 15.07.2011
 *      Author: snow
 */

#include "SparKonto.h"
#include "Konto.h"

SparKonto::SparKonto(float z, const string& name, unsigned long nr , double stand )
    : Konto(name, nr, stand), zins(z){
    ;
}


void SparKonto::display() const{
    Konto::display();

    cout << "Habenzinssatz: " << zins << endl;
}

/*
 * SparKonto.h
 *
 *  Created on: 15.07.2011
 *      Author: snow
 */

#ifndef SPARKONTO_H_
#define SPARKONTO_H_

#include "Konto.h"


class SparKonto : public Konto {

private:
    float zins;

public:
    SparKonto(float z = 0.02F, const string& name = "Unbekannt", unsigned long nr = 1111111L, double stand = 0.0);
    ~SparKonto(){}

    void setZins(float z){ zins = z;}
    float getZins() const{ return zins;}



    void display() const;
};

#endif /* SPARKONTO_H_ */

/*
 * konto_test.cpp
 *
 *  Created on: 15.07.2011
 *      Author: snow
 */

#include "Konto.h"
#include "GiroKonto.h"
#include "SparKonto.h"

int main(int argc, char *argv[]){
    GiroKonto giro;
    SparKonto spar;

    giro.display();

    spar.display();

    return 0;
}

Der Compiler gibt mir folgende Fehlermeldung:
Fehlermeldung   
Code:
snow@host:~/Code/LuPanwenden/Aufgaben$ g++ -o konto_test konto_test.cpp SparKonto.cpp GiroKonto.cpp
/tmp/ccIlAwhQ.o: In function `SparKonto::display() const':
SparKonto.cpp:(.text+0x66): undefined reference to `Konto::display() const'
/tmp/ccaa3jxM.o: In function `GiroKonto::display() const':
GiroKonto.cpp:(.text+0x76): undefined reference to `Konto::display() const'
collect2: ld returned 1 exit status


Also in kurz:
Giro/Spar-Konto erben Konto. Konto besitzt eine Methode
Code:
void display() const;
In GiroKonto redifinier ich
Code:
void display() const;
und will in dieser Methode per
Code:
Konto::display();
das "alte display() aufrufen, worauf mir der Compiller eine Fehlermeldung gibt.
Leider kann ich nicht nachvollziehen wo das Problem liegt...
Vielleicht kann mir ja wer weiterhelfen
Snow! ist offline   Mit Zitat antworten
Alt 15.07.11, 18:25   #2 (permalink)
Senior Member
 
Registriert seit: 13.07.08
enkore Leistung: K 6-3enkore Leistung: K 6-3enkore Leistung: K 6-3
Likes: 85
Standard

Und welchen Fehler erhälst du?
__________________
"It is the human race! The deterioration of the spirit of man. Man undermining himself, causing a self-willed, self-imposed, self-evident self-destruction."
+++ BREAKING +++ Troll ertrinkt im Planschbecken +++
enkore ist offline   Mit Zitat antworten
   
HaBOT
 
- Anzeige -

Werbung ist gerade online    
Alt 15.07.11, 18:34   #3 (permalink)
Themenstarter
 
Registriert seit: 09.07.11
Snow! Leistung: Facit NTK
Likes: 0
Standard

Zitat:
Und welchen Fehler erhälst du?

Siehe Spoiler
Snow! ist offline   Mit Zitat antworten
Alt 15.07.11, 18:38   #4 (permalink)
 
Registriert seit: 06.06.09
Thunderb0lt Leistung: 8086
Likes: 6
Standard

Konto::display() hast du in Konto.cpp definiert, aber die gibst du g++ nicht mit. Daher findet er die Methode beim linken nicht.
__________________
A good programmer looks both ways before crossing a one-way street.

Die Politik kann uns nur für so dumm verkaufen, wie wir sind.
Thunderb0lt ist offline   Mit Zitat antworten
Alt 15.07.11, 19:09   #5 (permalink)
Themenstarter
 
Registriert seit: 09.07.11
Snow! Leistung: Facit NTK
Likes: 0
Standard

Ok daran lag es :/. Jetzt komm ich mir zwar dumm vor aber trotzdem danke.
Snow! ist offline   Mit Zitat antworten
Antwort
   
- Anzeige -

Werbung ist gerade online    

[HaBo] » Software Home » Code Kitchen » C++ Klassenhierachie Fehler beim Kompilieren
Themen-Optionen
Ansicht

Forumregeln
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks sind aus
Pingbacks sind aus
Refbacks sind aus



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61