Copy-Constructor

M

Marco Kunze

Hi,

I have the following problem:

I am trying to write a small Matrix-class:

class Matrix {
public:
Matrix(int r, int c);
Matrix(Matrix& mat);
Matrix operator* (Matrix mat);
};

Now, when I do this:

Matrix m4 = m1;
m4 = m1 * m1;

everything works fine. But if I try to shorten it to

Matrix m4 = m1 * m1;

then the compiler says:

Test.cpp: In function `int main(int, char**)':
Test.cpp:61: error: no matching function for call to
`Matrix::Matrix(Matrix)'
Matrix.h:17: error: candidates are: Matrix::Matrix(Matrix&)

It would be great if someone could tell me my mistake, I don't see
anything obvious :(

Thank you.

Marco
 
V

Victor Bazarov

Marco Kunze said:
I have the following problem:

I am trying to write a small Matrix-class:

class Matrix {
public:
Matrix(int r, int c);
Matrix(Matrix& mat);
^^^^^^^^^^^^^^^^^^^^
Change this to

Matrix(Matrix const& mat);
Matrix operator* (Matrix mat);

Change this to

Matrix operator* (Matrix const& mat) const;

And read more about const-correctness of your code.
};

Now, when I do this:

Matrix m4 = m1;
m4 = m1 * m1;

everything works fine. But if I try to shorten it to

Matrix m4 = m1 * m1;

then the compiler says:

Test.cpp: In function `int main(int, char**)':
Test.cpp:61: error: no matching function for call to
`Matrix::Matrix(Matrix)'
Matrix.h:17: error: candidates are: Matrix::Matrix(Matrix&)

It would be great if someone could tell me my mistake, I don't see
anything obvious :(

See above

V
 
A

abc

HI,

The code is ok with my VC++2003ToolKit C/C++ complier.
Maybe your C++ complier isn't compatiable with ISO14882:1999 standard.
 
A

ashok.anbalan

Victor said:
^^^^^^^^^^^^^^^^^^^^
Change this to

Matrix(Matrix const& mat);


Change this to

Matrix operator* (Matrix const& mat) const;

And read more about const-correctness of your code.


See above

V
 
A

ashok.anbalan

Hi Victor,

The solution works fine. Thanks.

Can you tell us if there are a generic set of guidelines to be followed
for const-correctness that will obviate errors such as in Marco's
program?

Thanks,
Ashok
 
K

Karl Heinz Buchegger

Hi Victor,

The solution works fine. Thanks.

Can you tell us if there are a generic set of guidelines to be followed
for const-correctness that will obviate errors such as in Marco's
program?

Simple.
Make everything const unless there is a need to make it non const or
things are passed per value.
const should really be the default.
 
R

Richard Herring

Marco Kunze said:
Hi,

I have the following problem:

I am trying to write a small Matrix-class:

class Matrix {
public:
Matrix(int r, int c);
Matrix(Matrix& mat);
Matrix operator* (Matrix mat);
};

Now, when I do this:

Matrix m4 = m1;
m4 = m1 * m1;

everything works fine. But if I try to shorten it to

Matrix m4 = m1 * m1;

then the compiler says:

Test.cpp: In function `int main(int, char**)':
Test.cpp:61: error: no matching function for call to
`Matrix::Matrix(Matrix)'
Matrix.h:17: error: candidates are: Matrix::Matrix(Matrix&)

It would be great if someone could tell me my mistake, I don't see
anything obvious :(

Thank you.
HI,

The code is ok with my VC++2003ToolKit C/C++ complier.[/QUOTE]

Well, it shouldn't be. The offending line is attempting to bind a
temporary to a non-constant reference.
Maybe your C++ complier isn't compatiable with ISO14882:1999 standard.

And maybe *yours* isn't?
 
R

Richard Herring

Karl Heinz Buchegger said:
Simple.
Make everything const unless there is a need to make it non const or
things are passed per value.
const should really be the default.

Const-correctness isn't the only thing that's potentially wrong with
Marco's code, though since he didn't display the implementation of his
class we can't be certain. The warning sign is that he had a
user-defined copy constructor, but not a corresponding assignment
operator, nor a user-defined destructor:
class Matrix {
public:
Matrix(int r, int c);
Matrix(Matrix& mat);
Matrix operator* (Matrix mat);
};


Matrix m4 = m1;
m4 = m1 * m1;

Matrix m4 = m1 * m1;

If the implementation uses something like std::vector there's probably
no problem, and the user-defined copy ctor may not be needed at all. IN
that case it's better to take what the compiler gives you for nothing.

<spooky music>

But if the implementation involves dynamic allocation and raw pointers,
I foresee memory leaks, double deletion and undefined behaviour in the
not too distant future...
 
M

Matthias Kaeppler

Richard said:
Well, it shouldn't be. The offending line is attempting to bind a
temporary to a non-constant reference.

So, for the assignment, the copy constructor is called, which takes a
non-const reference? Why isn't the generated assignment operator called?
 
R

Rolf Magnus

Matthias said:
So, for the assignment, the copy constructor is called, which takes a
non-const reference?
No.

Why isn't the generated assignment operator called?

What makes you believe it isn't?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top