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
Click For Summary

Discussion Overview

The discussion revolves around resolving an undefined reference error encountered in a user-defined C++ matrix class constructor. Participants explore potential causes related to class design, specifically focusing on copy constructors and linker issues.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a matrix constructor and describes the error message received when trying to instantiate the class.
  • Another participant suggests that the error may stem from a missing implementation of the copy constructor, referencing the "law of the big three" in C++ programming.
  • Some participants argue that the issue is likely a linking problem, indicating that the object file containing the implementation of the Matrix's member functions may not have been included in the linking stage.
  • There is a discussion about whether the default copy constructor could lead to a rule-of-three error if a user-defined constructor is present without an accompanying copy constructor.
  • One participant shares their experience that linker errors often arise from failing to implement specified functions rather than from linking issues.
  • A participant mentions testing a similar constructor without defining a copy constructor, noting that it worked properly, suggesting system/compiler dependency.
  • Another participant emphasizes the need for the original poster to confirm if the copy constructor was explicitly defined.
  • A later reply indicates that the original poster confirmed they had declared a copy constructor but did not implement it, resolving their issue.

Areas of Agreement / Disagreement

Participants generally agree that the undefined reference error is likely related to the copy constructor and linking issues. However, there are differing views on the specifics of the problem and the implications of the rule of three.

Contextual Notes

Some participants note that additional information, such as the full class definition and compilation details, would be necessary for a more accurate diagnosis of the issue.

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

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
Replies
9
Views
2K