wo ist mein fehler (allg. konstruktor)
-
// {\em cppbuch/k5/ort1main.cpp} #include"ort1.h" #include<iostream> using namespace std; int main() { Ort1 einOrt1; //einOrt1.aendern(100, 200); einOrt1(100, 200); cout << "Der Ort hat die Koordinaten x = " << einOrt1.X() << " und y = " << einOrt1.Y() << endl; system("Pause"); }
// {\em cppbuch/k5/ort1.cpp} #include"ort1.h" #include<iostream> using namespace std; Ort1::Ort1(int x, int y) { xKoordinate = x; yKoordinate = y; } int Ort1::X() const { return xKoordinate;} int Ort1::Y() const { return yKoordinate;} void Ort1::aendern(int x, int y) { xKoordinate = x; yKoordinate = y; cout << "Ort1-Objekt geändert! x = " << xKoordinate << " y = " << yKoordinate << endl; }
// {\em cppbuch/k5/ort1main.cpp} #include"ort1.h" #include<iostream> using namespace std; int main() { Ort1 einOrt1; //einOrt1.aendern(100, 200); einOrt1(100, 200); //FEHLER cout << "Der Ort hat die Koordinaten x = " << einOrt1.X() << " und y = " << einOrt1.Y() << endl; system("Pause"); }
-
ich meinte:
// {\em cppbuch/k5/ort1.h } #ifndef ort1_h #define ort1_h ort1_h class Ort1 { // 1. Version public: int X() const; int Y() const; Ort1(); void aendern(int x, int y); // x,y = neue Werte private: int xKoordinate, yKoordinate; }; #endif // ort1_h
-
[cpp]// {\em cppbuch/k5/ort1main.cpp}
#include"ort1.h"
#include<iostream>
using namespace std;int main() {
Ort1 einOrt1(100, 200);cout << "Der Ort hat die Koordinaten x = "
<< einOrt1.X() << " und y = "
<< einOrt1.Y() << endl;
system("Pause");
}[/cpp][edit]
du musst naturlich den ctor auch deklarieren// *.h class Ort1 { public: Ort1(int x, int y); // ...