Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Matrix Constructor Help

  1. Oct 27, 2008 #1
    Hi, I have a matrix class which I defined myself constructor is:

    Code (Text):
     Matrix::Matrix(unsigned s1, unsigned s2, unsigned s3, unsigned s4)
       s1_ = s1; s2_ = s2; s3_ = s3; s4_ = s4;
       data_ = new double[s1*s2*s3*s4];
    I try to call it using:

    Code (Text):
    Matrix field(NX,NY,NU,NV);
    Where NX, NY, NU, NV are all declared as global variables of type unsigned, which have their values set prior to this. It gives me this error message:

    Code (Text):
    /tmp/cc06NW6n.o: In function `main':
    solve.cpp:(.text+0x137d): undefined reference to `Matrix::Matrix(Matrix const&)'
    collect2: ld returned 1 exit status
    Anyone have any ideas how to fix this?

    Thanks in advance.
  2. jcsd
  3. Oct 27, 2008 #2

    D H

    Staff: Mentor

    Show your class. Somewhere in your code you are calling the Matrix copy constructor. You are doing something akin to bar = new Matrix(foo);, which foo is a Matrix object.

    It looks like you violated the law of the big three: If you find that you need to explicitly define for some class any one of the destructor, the copy constructor, or the assignment operator, you probably should be defining all three.
  4. Oct 27, 2008 #3


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    It looks more like a link problem -- when linking the program, he didn't pass in the object file containing the implementation of Matrix's member functions.
  5. Oct 27, 2008 #4

    Code (Text):
    undefined reference to `Matrix::Matrix(Matrix const&)'
    collect2: ld returned 1 exit status
    is a linker error. Conceivably it could be due to GCC's default copy constructor giving rise to a rule-of-three error, but my first instinct would be to focus on the link stage.

    That said, unless we get more info (class definition, command line options to GCC, whether or not he's using virtual construction with a clone() method, etc) from the OP there's not a lot can be done to solve the problem.
  6. Oct 27, 2008 #5

    D H

    Staff: Mentor

    From my experience these linker errors usually occur because the programmer failed to implement the specified function rather than having implemented the specified function but failing to provide the compiled object to the linker.

    At least he didn't get an error message like "undefined reference to `non-virtual thunk to
    Matrix::Matrix()'" or "undefined reference to vtable for Matrix".
  7. Oct 27, 2008 #6
    I have done a test with a similar user defined constructor withOUT defining the copy constructor, and the copy constructor still works properly. However, I believe this is system/compiler dependent and have to agree with DH in that when a specific constructor is defined, we should explicitly define the copy constructor (if it is used), and perhaps even the constructor with no arguments (similar to the default one).

    Could NeoDevin please confirm if the copy constructor was explicitly defined?
  8. Oct 28, 2008 #7


    User Avatar

    A compiler defined default constructor is provided that does a shallow copy if no default constructor is provided by the user. So the linker error wouldn't occur if the copy constructor haven't been declared at all by the user.
  9. Oct 28, 2008 #8

    D H

    Staff: Mentor

    Exactly. That is why the very first thing I said was "show us your class". I suspect he declared a copy constructor and didn't implement it, didn't compile it, or didn't tell the linker to link the compiled object file into the program.
  10. Oct 28, 2008 #9
    That's exactly what I did, thank you. Problem solved.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: C++ Matrix Constructor Help
  1. C++ Matrix Solver (Replies: 0)