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

  • Context: C/C++ 
  • Thread starter Thread starter NeoDevin
  • Start date Start date
  • Tags Tags
    C++ Matrix
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 8K views
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.
 
Physics 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.
 
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.
 
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.