Habe da ein Problem mit dem Kopierkonstruktor in C++:
Ich habe ihn so deklariert:
|
C-/C++-Quelltext
|
1
|
Matrix(const Matrix& other);
|
So ist dann meine definition:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Matrix::Matrix(const Matrix& other):
n(other.n), m(other.m)
{
if (&other!=this)
{
const Matrix *pother = &other;
for (int i=0; i<m; i++)
{
add_level();
for (int j=0; j<n; j++)
append2D(i, new DeriveValue(*static_cast<DeriveValue*>(pother->get2D(i,j))));
}
for (int i=0; i<nodes.len(); i++)
{
nodes.append(new Node(static_cast<Node*>(pother->nodes.get(i))));
}
}
}
|
Der Compiler (g++) moppert bei den Stellen get(i) und get2D(i,j) die geweils von einer Basisklasse vererbt sind:
|
Quellcode
|
1
2
3
4
|
Matrix.cpp: In copy constructor ‘Matrix::Matrix(const Matrix&)’:
Matrix.cpp:21: error: passing ‘const Matrix’ as ‘this’ argument of ‘Entity* List2D::get2D(int, int)’ discards qualifiers
Matrix.cpp:26: error: passing ‘const ListMain’ as ‘this’ argument of ‘Entity* ListMain::get(int)’ discards qualifiers
Matrix.cpp: In member function ‘void Matrix::zip_jacobi(Matrix&, Matrix&)’:
|
Die beiden Funktionen verändern natürlich nichts, sondern geben nur zurück. Deklarationen:
|
C-/C++-Quelltext
|
1
2
|
Entity* get2D(int x, int y);
Entity* get(int i);
|
Meine bisherigen Recherchen haben ergeben, dass es etwas mit der Aussage über const zu tun hat. Wenn ich die jedoch weglasse gibt es andere Fehler, die unangenehmer zu sein scheinen:
|
Quellcode
|
1
2
3
|
main.cpp:53: error: no matching function for call to ‘Matrix::Matrix(Matrix)’
../inc/Matrix.h:30: note: candidates are: Matrix::Matrix(Matrix&)
../inc/Matrix.h:28: note: Matrix::Matrix(int, int)
|
Für Hilfe und Erklärungen wäre ich sehr dankbar
hf
Arthur