#1 14. September 2014 Hallo, Ich habe eine Matrix, worin ich die Daten halte mit dem double**-Zeiger. Der zeigt auf die Rheihe und dann auf die Spalte. Nun möchte ich folgende Zeile umsetzen: Matrix.on(4,5) = 20; Ich vermute mal, man macht das so: 1. Die Adresse vom (4,5) als einen doppelten Zeiger zurück geben. 2. Den = Operator überladen und an diese Adresse die 20 schreiben. Nur kann ich das im Code nicht so umsetzen: Code: int** Matrix::on(int rheihe, int spalte){ return **this->Werte[rheihe][spalte] //geht nicht..... } void Operantor=(double wert){ this->Werte[ ][ ] = wert; } Danke für jeden Tipp. + Multi-Zitat Zitieren
#2 14. September 2014 Zuletzt bearbeitet: 15. September 2014 AW: c++ Einer Funktion ein Wert zuweisen Wenn du das umbedingt so haben willst ^^ Möglich isses auf alle Fälle: mit nem wrapper Spoiler Code: #include <iostream> class Matrix { public: class Cell; private: const size_t rows; const size_t cols; Cell* cells; public: const static int out_of_bounds = -1; Matrix(const size_t, const size_t); ~Matrix(void); Cell& on(const size_t, const size_t); class Cell { double value; public: Cell(void); void operator= (const double v); double get_value(void) const; double set_value(const double); friend std::ostream& operator<< (std::ostream&, const Matrix::Cell&); }; }; // --------------------------------------- Matrix::Matrix(const size_t _rows, const size_t _cols) : rows(_rows), cols(_cols) { cells = new Cell[rows * cols]; } Matrix::~Matrix(void) { delete[] cells; } Matrix::Cell& Matrix::on(const size_t row, const size_t col) { if (row >= rows || col >= cols) throw Matrix::out_of_bounds; return cells[row * cols + col]; } // --------------------------------------- Matrix::Cell::Cell(void) { value = 0.; } void Matrix::Cell::operator= (const double _value) { value = _value; } double Matrix::Cell::get_value(void) const { return value; } double Matrix::Cell::set_value(const double _value) { double old_value = value; value = _value; return old_value; } std::ostream& operator<< (std::ostream& os, const Matrix::Cell& c) { os << c.get_value(); return os; } // --------------------------------------- int main(void) { Matrix m { 5, 5 }; for (unsigned r = 0; r < 5; ++r) for (unsigned c = 0; c < 5; ++c) m.on(r, c) = r + c; for (unsigned r = 0; r < 5; ++r) for (unsigned c = 0; c < 5; ++c) std::cout << "[" << r << "] [" << c << "] = " << m.on(r, c) << std::endl; return 0; } mit nem einfachen pointer Code: #include <iostream> class Matrix { const size_t rows; const size_t cols; double* cells; public: const static int out_of_bounds = -1; Matrix(const size_t, const size_t); ~Matrix(void); double* on(const size_t, const size_t); }; // --------------------------------------- Matrix::Matrix(const size_t _rows, const size_t _cols) : rows(_rows), cols(_cols) { cells = new double[rows * cols](); } Matrix::~Matrix(void) { delete[] cells; } double* Matrix::on(const size_t row, const size_t col) { if (row >= rows || col >= cols) throw Matrix::out_of_bounds; return &(cells[row * cols + col]); } // --------------------------------------- int main(void) { Matrix m { 5, 5 }; for (unsigned r = 0; r < 5; ++r) for (unsigned c = 0; c < 5; ++c) *m.on(r, c) = r + c; for (unsigned r = 0; r < 5; ++r) for (unsigned c = 0; c < 5; ++c) std::cout << "[" << r << "] [" << c << "] = " << *m.on(r, c) << std::endl; return 0; } Hab der Einfachheit halber auf ein multidimensionales array oder std::vector verzichtet. + Multi-Zitat Zitieren