Koordinaten werden wirr gesetzt?
-
Hi,
ich komme wieder einmal mit einem Problem auf Euch zu:
ich beschäftige mich gerade mit dem Thema Klassen und stoße dabei auf eine Ungereimtheit, die ich nicht verstehe, so sehr ich mir auch den Kopf zerbreche: die durch den User definierten Koordinaten werden einfach nicht angezeigt sondern stattdessen eine ganz wirre Zahl, wie wenn ich eine random Funktion eingebaut hätte... Hier ist meine Programmausgabe:
Hi Christmas King! Where should your spacecraft start? Please provide X: 58
Now please the Y: 90
Defining start values of your spacecraft
Attributes of Jacky Frost
Position on screen:
X: 140
Y: 320
0x477864Attributes:
Height: 25
Speed: 50
Energy: 1000
Setting position of your spacecraft...
Valid coordinates! Proceeding...
Coordinates set!
Attributes of Christmas King
Position on screen:
X: 8523608
Y: 8519872
0x477864Attributes:
Height: 25
Speed: 50
Energy: 1000
Sapcecraft destructed
Sapcecraft destructedProcess returned 0 (0x0) execution time : 3.126 s
Press any key to continue.Hier der Inhalt der einzelnen Dateien:
// Classes.hppclass spacecraft { private: // Position int xpos; int ypos; // Attributes int height; int speed; int energy; public: // Constructors spacecraft(); spacecraft(int xpos, int ypos); // prototype of function (Prototype, content, execute) // Object - Functions void showData(); // Destructor ~spacecraft(); };
// Functions.cpp
#include <iostream> #include "Classes.hpp" using namespace std; // IMPORTANT NOTE: Constructors are functions and work the same way // Initializing of spacecraft using constructor spacecraft::spacecraft() // defines the position and attributes of the spacecraft automatically { cout << "Defining start values of your spacecraft" << endl; xpos = 140; ypos = 320; height = 25; speed = 50; energy = 1000; } spacecraft::spacecraft(int xpos, int ypos) // displays spacecraft at position the user defines; will be transferred to this constructor in the main-function { cout << "Setting position of your spacecraft..." << endl; // Defining position values if(xpos > 0 && xpos < 800 && ypos > 0 && ypos < 600) // Screen resolution { cout << "Valid coordinates! Proceeding..." << endl; xpos = xpos; ypos = ypos; cout << "Coordinates set!" << endl; } else { cout << "Invalid position! Setting to 0!" << endl; xpos = 0; ypos = 0; } height = 25; speed = 50; energy = 1000; } void spacecraft::showData() { cout << "Position on screen:" << endl << "X: " << xpos << endl << "Y: " << ypos << endl << cout << "Attributes:" << endl << "Height: " << height << endl << "Speed: " << speed << endl << "Energy: " << energy << endl; } spacecraft::~spacecraft() { cout << "Sapcecraft destructed" << endl; }
// Spacewar.cpp
#include <iostream> #include "Spacecraft/Functions.cpp" using namespace std; int main() { // Pointers for instances of "spacecraft" spacecraft *christmasking = NULL; // Pointers point to position 0 in memory spacecraft *jackyfrost = NULL; //Attributes defined by user int xpos, ypos; // Asking Jacky Frost, where his spacecraft should start cout << "Hi Christmas King! Where should your spacecraft start? Please provide X: "; cin >> xpos; cout << "Now please the Y: "; cin >> ypos; // Spacecrafts are created and displayed jackyfrost = new spacecraft(); cout << "Attributes of Jacky Frost" << endl; jackyfrost->showData(); christmasking = new spacecraft(xpos, ypos); cout << "Attributes of Christmas King" << endl; christmasking->showData(); // Destruct spacecrafts delete christmasking; christmasking = NULL; delete jackyfrost; christmasking = NULL; return 0; }
Wäre für Antworten und die Lösung meines Knotens sehr dankbar! Wasl ist es wiedermal eine Kleinigkeit die ich übersehe; ist meistens so
Vielen Dank und LG Martin
-
@venezianer27 sagte in Koordinaten werden wirr gesetzt?:
xpos = xpos;
Fällt dir bei solchem Code nicht auf, dass das möglicherweise seltsam sein könnte und du besser mal nachforschst, was das genau bedeuten könnte? Ich würde mich da jedenfalls fragen, woher der Compiler wissen soll, dass das eine
xpos
etwas anderes bedeutet als das andere.
-
@venezianer27
Dein main könntest du verkürzen - man braucht nicht immer new zu verwenden! (man solltenew
überhaupt nicht verwenden!)int main() { //Attributes defined by user int xpos, ypos; // Asking Jacky Frost, where his spacecraft should start cout << "Hi Christmas King! Where should your spacecraft start? Please provide X: "; cin >> xpos; cout << "Now please the Y: "; cin >> ypos; spacecraft jackyfrost; cout << "Attributes of Jacky Frost" << endl; jackyfrost.showData(); spacecraft christmasking(xpos, ypos); cout << "Attributes of Christmas King" << endl; christmasking.showData(); }
Oder, wenn du die Schiffe im Freispeicher brauchst:
#include <memory> [...] int main() { //Attributes defined by user int xpos, ypos; // Asking Jacky Frost, where his spacecraft should start cout << "Hi Christmas King! Where should your spacecraft start? Please provide X: "; cin >> xpos; cout << "Now please the Y: "; cin >> ypos; auto jackyfrost = std::make_unique<spacecraft>(); cout << "Attributes of Jacky Frost" << endl; jackyfrost->showData(); auto christmasking = std::make_unique<spacecraft>(xpos, ypos); cout << "Attributes of Christmas King" << endl; christmasking->showData(); }
Wie du siehst, hast du in meinen Varianten auch kein
delete
, das du vergessen könntest.Und noch ein Rat: Header-Datei und die dazu passende cpp-Datei sollten (bis auf die Dateinamenserweiterung) schon denselben Namen haben, also z.B. spacecraft.h(xx/pp)? und spacecraft.c(pp/xx) statt classes.hpp und functions.cpp.
-
@Bashar Ich habe nachgeforscht und das auch schon verändert. Das ändert aber an der Ausgabe genau nichts. Ich habe versucht:
int x1;
int y1;Und dann im 2. Konstruktor x1 =xpos, y1=ypos
Das änderte genau gar nichts!
-
@wob Danke! ich werds nochmal vergleichen und verbessern. Aber wie ich schon Bosh antwortete, egal was ich wie drehe er gibt mir immer dieselbe Ausgabe...
-
@venezianer27 * Bashar
-
Wie sieht denn dein
spacecraft::spacecraft(int xpos, int ypos)
jetzt aus und wie ist die Header-Datei dazu?PS: Du kannst deine Beiträge editieren über die 3 Punkte rechts von "Zitieren". Dann könntest du Bosh -> Bashar berichtigen und brauchst dafür kein 2. Posting.
-
@venezianer27
Der Code ist so wie gepostet nicht compilierfähig. Bitte poste exakt den Code, der das geschilderte Problem verursacht. Vielleicht hast du gerade die Zuweisung in irgendeiner Datei korrigiert, die gar nicht mitcompiliert wird.
-
Im ersten Post Functions.cpp Zeile 46.