473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Copy-Constructor

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
Jul 23 '05 #1
9 1599
"Marco Kunze" <ma*****@gmx.de> wrote in message
news:38*************@individual.net...
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
Jul 23 '05 #2
abc
HI,

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

"Marco Kunze" <ma*****@gmx.de> wrote in message
news:38*************@individual.net...
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

Jul 23 '05 #3

Victor Bazarov wrote:
"Marco Kunze" <ma*****@gmx.de> wrote in message
news:38*************@individual.net...
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


Jul 23 '05 #4
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

Jul 23 '05 #5
as***********@gmail.com wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6
In message <d0***********@mail.cn99.com>, abc <ab*@abc.com> writes

[please don't top-post]

"Marco Kunze" <ma*****@gmx.de> wrote in message
news:38*************@individual.net...
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.


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?

--
Richard Herring
Jul 23 '05 #7
In message <42***************@gascad.at>, Karl Heinz Buchegger
<kb******@gascad.at> writes
as***********@gmail.com wrote:

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.


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...

--
Richard Herring
Jul 23 '05 #8
Richard Herring wrote:

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?

--
Matthias Kaeppler
Jul 23 '05 #9
Matthias Kaeppler wrote:
Richard Herring wrote:

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?


No.
Why isn't the generated assignment operator called?


What makes you believe it isn't?

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
7
by: Richard Forester | last post by:
Hello. I need some help understanding what goes on when an array is copied. I create 2 arrays and copy one to the other: int pins = {9, 3, 7, 2}; int copy = new int; for (int i = 0; i !=...
5
by: lion | last post by:
in .net, if you set annstance-A of a class equal to another instance-B, a pointer will add to B, but if i want to create a copy of B instead of pointer, how to operate? Note:serialization...
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
2
by: flamexx7 | last post by:
http://www.rafb.net/paste/results/V3TZeb28.html In this code, why copy constructor is not called while returning object from no_arg() . I was trying to find answer in C++ Standard. and there it's...
12
by: Mark E. Fenner | last post by:
Hello all, I have a code where my inner loop looks like: allNew = for params in cases: newObj = copy(initialObject) newObj.modify(params) allNew.append(newObj) return allNew
3
by: maheshkadam | last post by:
Hi friends I am new to perl so please guide me. I have one application which created backup log file every day.But it appends that file so you can see logs for different day in one file only. ...
3
by: yoma | last post by:
python version 2.5 in module copy we all know that copy have two method: copy() and deepcopy(). and the explain is - A shallow copy constructs a new compound object and then (to the extent...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.