Copy-Constructor

Discussion in 'C++' started by Marco Kunze, Feb 28, 2005.

  1. Marco Kunze

    Marco Kunze Guest

    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
    Marco Kunze, Feb 28, 2005
    #1
    1. Advertising

  2. "Marco Kunze" <> wrote in message
    news:...
    > 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
    Victor Bazarov, Feb 28, 2005
    #2
    1. Advertising

  3. Marco Kunze

    abc Guest

    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" <> wrote in message
    news:...
    > 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
    abc, Mar 1, 2005
    #3
  4. Marco Kunze

    Guest

    Victor Bazarov wrote:
    > "Marco Kunze" <> wrote in message
    > news:...
    > > 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
    , Mar 1, 2005
    #4
  5. Marco Kunze

    Guest

    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
    , Mar 1, 2005
    #5
  6. 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
    Karl Heinz Buchegger, Mar 1, 2005
    #6
  7. In message <d017ia$14jm$99.com>, abc <> writes

    [please don't top-post]
    >
    >"Marco Kunze" <> wrote in message
    >news:...
    >> 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
    Richard Herring, Mar 1, 2005
    #7
  8. In message <>, Karl Heinz Buchegger
    <> writes
    > 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
    Richard Herring, Mar 1, 2005
    #8
  9. 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
    Matthias Kaeppler, Mar 1, 2005
    #9
  10. Marco Kunze

    Rolf Magnus Guest

    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?
    Rolf Magnus, Mar 1, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    8
    Views:
    4,897
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    465
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    576
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,227
  5. cinsk
    Replies:
    35
    Views:
    2,603
    James Kanze
    Oct 11, 2010
Loading...

Share This Page