C/C++ How to Fix Undefined Reference Error in C++ Matrix Constructor

  • Thread starter Thread starter NeoDevin
  • Start date Start date
  • Tags Tags
    C++ Matrix
AI Thread Summary
The discussion revolves around a linker error encountered when trying to instantiate a custom Matrix class in C++. The error message indicates an undefined reference to the copy constructor of the Matrix class. Participants highlight that this issue typically arises when a user-defined constructor, destructor, or copy constructor is declared but not implemented, leading to the need for the rule of three to be followed. The error suggests a potential linking problem, where the object file containing the implementation of the Matrix class is not included during the linking stage. Participants emphasize the importance of providing implementations for all special member functions if any are explicitly defined. The conversation concludes with the original poster confirming that the issue was resolved after addressing the copy constructor implementation.
NeoDevin
Messages
334
Reaction score
2
Hi, I have a matrix class which I defined myself constructor is:

Code:
 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:
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:
/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.
 
Technology news on Phys.org
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 anyone of the destructor, the copy constructor, or the assignment operator, you probably should be defining all three.
 
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.
 
D H said:
It looks like you violated the law of the big three: If you find that you need to explicitly define for some class anyone of the destructor, the copy constructor, or the assignment operator, you probably should be defining all three.

This

Code:
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.
 
shoehorn said:
This 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.
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".
 
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?
 
D H said:
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".

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.
 
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.
 
D H said:
I suspect he declared a copy constructor and didn't implement it

That's exactly what I did, thank you. Problem solved.
 

Similar threads

Back
Top