help...



  • hallo habe hier ein kleines tool gebaut bei dem ich für punkte, polyline, kreis usw. werte angeben kann dann entscheide ob ich sie rotieren lasse, skalierung oder wie auch immer... nun hab ich das problem dass ich bei der polylinie / polygon immer ein memory-error bekomm und das programm nicht ausgeführt wird.. aber warum?? hier mal der quelltext und der fehler...

    Object.cpp
    C:\2.Semester\Informatik\GraficTool\Object.cpp(318) : warning C4700: Lokale Variable 'result' wurde ohne Initialisierung verwendet
    C:\2.Semester\Informatik\GraficTool\Object.cpp(343) : warning C4700: Lokale Variable 'result' wurde ohne Initialisierung verwendet
    Linker-Vorgang läuft...

    GraficTool.exe - 0 Fehler, 2 Warnung(en)

    #include "Matrix.h"
    #include "Object.h"
    #include <iostream.h>
    #include <vector>

    Object* Object::operator* (Matrix mat)
    {
    return 0;
    }

    void Object::PrintInformation()
    {
    }

    ///////////////////////////////////////////////////////////////////
    // Point
    ///////////////////////////////////////////////////////////////////

    // Standardkonstruktor
    Point::Point()
    {
    x = y = 0;
    }

    // Konstruktor
    Point::Point(float PosX, float PosY)
    {
    x = PosX;
    y = PosY;

    }

    // Destruktor
    Point::~Point()
    {
    }

    // Kreiert einen Punkt aus den Angaben
    Point* MakePoint(float PosX, float PosY)
    {
    Point* result = new Point(PosX, PosY);
    return result;
    }

    // Multiplikation Punkte mit Matrix
    Object* Point::operator* (Matrix mat)
    {

    Point* result = this;
    result->x = mat.M[0][0] * this->x + mat.M[1][0] * this->y + mat.M[2][0];
    result->y = mat.M[0][1] * this->x + mat.M[1][1] * this->y + mat.M[2][1];
    return result;
    }

    // Ausgabe der Punktinformationen
    void Point::PrintInformation()
    {
    cout << "(" << x << ", " << y << ")\n";
    }

    void Point::Print()
    {
    cout << "(" << x << ", " << y << ")";
    }

    ///////////////////////////////////////////////////////////////////
    // Line
    ///////////////////////////////////////////////////////////////////

    // Konstruktor
    Line::Line(float x1, float y1, float x2, float y2)
    {
    Pnt1 = MakePoint(x1, y1);
    Pnt2 = MakePoint(x2, y2);

    }

    Line::Line()
    {
    Pnt1 = MakePoint(0, 0);
    Pnt2 = MakePoint(0, 0);

    }

    // Destruktor
    Line::~Line()
    {
    }

    // Multiplikation Line mit Matrix
    Object* Line::operator* (Matrix mat)
    {
    Line* result = this;
    result->Pnt1 = static_cast<Point*>(*Pnt1 * mat);
    result->Pnt2 = static_cast<Point*>(*Pnt2 * mat);
    return result;
    }

    // Ausgabe Punktinfo
    void Line::PrintInformation()
    {
    Pnt1->Print();
    cout << " - ";
    Pnt2->Print();
    cout << "\n";
    }

    ///////////////////////////////////////////////////////////////////
    // Polyline
    ///////////////////////////////////////////////////////////////////

    // Konstruktor
    Polyline::Polyline()
    {

    }

    // Destruktor
    Polyline::~Polyline()
    {
    }

    // Neuer Punkt hinzufügen
    void Polyline::AddPoint(float x, float y)
    {
    Points.push_back(MakePoint(x, y));
    }

    // Multiplikation Linie mit Matrix
    Object* Polyline::operator* (Matrix mat)
    {
    Polyline* result = this;
    for(int i = 0; i < Points.size(); i++)
    {
    result->Points[i] = static_cast<Point*>(*Points[i] * mat);
    }
    return result;
    }

    // Ausgabe der Punktinformationen
    void Polyline::PrintInformation()
    {
    for(int i = 0; i < Points.size(); i++)
    {
    Points[i]->Print();
    cout << ";";
    }
    cout << "\n";
    }

    ///////////////////////////////////////////////////////////////////
    // Polygon - Polygonklasse
    ///////////////////////////////////////////////////////////////////

    // Konstruktor

    Polygon::Polygon()
    {
    }

    // Destruktor
    Polygon::~Polygon()
    {
    }

    // Neuer Punkt hinzufügen
    void Polygon::AddPoint(float x, float y)
    {
    Points.push_back(MakePoint(x, y));
    }

    // Multiplikation Line mit Matrix
    Object* Polygon::operator* (Matrix mat)
    {
    Polygon* result = this;
    for(int i = 0; i < Points.size(); i++)
    {
    result->Points[i] = static_cast<Point*>(*Points[i] * mat);
    }
    return result;
    }

    // Ausgabe Punktinfo
    void Polygon::PrintInformation()
    {
    for(int i = 0; i < Points.size(); i++)
    {
    Points[i]->PrintInformation();
    cout << ";";
    }
    cout << "\n";
    }

    ///////////////////////////////////////////////////////////////////
    // Kreis
    ///////////////////////////////////////////////////////////////////

    // Konstruktor
    Circle::Circle(float xPos, float yPos, float r)
    {
    x = xPos;
    y = yPos;
    radius = r;

    }

    Circle::Circle()
    {
    }

    // Destruktor
    Circle::~Circle()
    {
    }

    // Multiplikation Line mit Matrix
    Object* Circle::operator* (Matrix mat)
    {
    Circle* result = this;
    result->x = this->x + mat.M[2][0];
    result->y = this->y + mat.M[2][1];
    return result;
    }

    // Ausgabe der Punktinformationen
    void Circle::PrintInformation()
    {
    cout << "(" << x << ", " << y << "), " << radius << "\n";
    }

    Matrix MakeTranslation()
    {
    float x, y;
    cout << "Translation in X-Richtung?\n";
    cin >> x;
    cout << "Translation in Y-Richtung?\n";
    cin >> y;
    return Translation(x, y);
    }

    Matrix MakeScalation()
    {
    float x, y;
    cout << "Skalierung in X-Richtung?\n";
    cin >> x;
    cout << "Skalierung in Y-Richtung?\n";
    cin >> y;
    return Scalation(x, y);
    }

    Matrix MakeRotation()
    {
    float alpha;
    cout << "Winkel der Rotation?\n";
    cin >> alpha;
    return Rotation(alpha);
    }

    Object* InputPoint()
    {
    float x, y;
    Object* result;

    cout << "X-Position:\n";
    cin >> x;
    cout << "Y-Position:\n";
    cin >> y;

    result = new Point(x, y);
    return result;
    }

    Object* InputLine()
    {
    float x1, y1, x2, y2;
    Object* result;

    cout << "X-Position des Startpunktes:\n";
    cin >> x1;
    cout << "Y-Position des Startpunktes:\n";
    cin >> y1;
    cout << "X-Position des Endpunktes:\n";
    cin >> x2;
    cout << "Y-Position des Endpunktes:\n";
    cin >> y2;

    result = new Line(x1, y1, x2, y2);
    return result;
    }

    Object* InputPolyline()
    {
    float count;
    float x, y;
    Object* result;

    cout << "Anzahl Punkte:\n";
    cin >> count;

    for(int i = 0; i < count; i++)
    {
    cout << "X-Position des " << i + 1 << ". Punktes:\n";
    cin >> x;
    cout << "Y-Position des " << i + 1 << ". Punktes:\n";
    cin >> y;
    static_cast <Polyline*> ( result ) -> AddPoint(x, y);

    }

    return result;
    }

    Object* InputPolygon()
    {
    float count;
    float x, y;
    Object* result;

    cout << "Anzahl Punkte:\n";
    cin >> count;

    for(int i = 0; i < count; i++)
    {
    cout << "X-Position des " << i + 1 << ". Punktes:\n";
    cin >> x;
    cout << "Y-Position des " << i + 1 << ". Punktes:\n";
    cin >> y;
    static_cast<Polygon*>(result)->AddPoint(x, y);
    }

    return result;
    }

    Object* InputCircle()
    {
    float x, y, r;
    Object* result;

    cout << "X-Position:\n";
    cin >> x;
    cout << "Y-Position:\n";
    cin >> y;
    cout << "Radius:\n";
    cin >> r;

    result = new Circle(x, y, r);
    return result;
    }



  • 1. codetags
    2. maybe mal nachschauen, was in betreffender vom compiler bemängelter zeile steht?
    3.

    Object* InputPolyline()
    {
    float count;
    float x, y;
    Object* result; //<- nicht initalisiertes objekt
    
    cout << "Anzahl Punkte:\n";
    cin >> count;
    
    for(int i = 0; i < count; i++)
    {
    cout << "X-Position des " << i + 1 << ". Punktes:\n";
    cin >> x;
    cout << "Y-Position des " << i + 1 << ". Punktes:\n";
    cin >> y;
    static_cast <Polyline*> ( result ) -> AddPoint(x, y); //obj bei funktionsaufruf nicht initalisiert
    }
    

    dir fehlt ein new.



  • ahso ich muss das result erst initialisiern also new result oder was??



  • hab ja schon gesehn dass in dieser zeile was falsch ist nur weiß ich ja leider nicht was... was soll ich nun mit diesem new bitte machen??



  • Ich weiss gar nicht, was du genau willst mit der Func.
    Maybe

    Object = new Polyline;
    

    , probier mal ob dir das hilft.
    BTW solltest du allen Speicher, den du mit new reservierst, wieder mit delete freigeben.



  • ich frage ja hier nach aus wie viel punkte die polyline bestehen soll... diese sollen dann gespeichert und weitergegeben werden...

    static_cast <Polyline*> ( result ) -> AddPoint(x, y);



  • du hattest recht hab es gerade selbst gemerkt... ich galub ich war heut einfach zu lange vor dem bildschirm hier.... vielen dank für deine schnelle hilfe!!!! danke, danke 👍 🙂 🙂 🙂



  • NP 🤡


Anmelden zum Antworten