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

  • C/C++
  • Thread starter NeoDevin
  • Start date
  • Tags
    C++ Matrix
In summary, the user defined constructor for Matrix does not work because the compiler default copy constructor was not defined.
  • #1
NeoDevin
334
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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".
 
  • #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?
 
  • #7
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.
 
  • #8
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.
 
  • #9
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.
 

1. What is a matrix constructor in C++?

A matrix constructor in C++ is a special function that is used to create and initialize matrices. It is used to allocate memory for the matrix and set its initial values. This allows for the creation of custom matrices with specific dimensions and data types.

2. How do I declare and initialize a matrix using a constructor in C++?

To declare and initialize a matrix using a constructor in C++, you first need to create a class for the matrix. Within this class, you can define a constructor function that takes in the desired dimensions and data type of the matrix. Within the constructor, you can use loops and other functions to initialize the values of the matrix.

3. Can I have multiple constructors for a matrix in C++?

Yes, it is possible to have multiple constructors for a matrix in C++. This is useful if you want to create matrices with different dimensions or data types. You can define each constructor with different parameters, allowing for more flexibility in creating matrices.

4. How do I access and modify specific elements in a matrix created with a constructor in C++?

To access and modify specific elements in a matrix created with a constructor in C++, you can use the indexing notation. This allows you to specify the row and column of the element you want to access or modify. You can also use loops and other functions to manipulate multiple elements at once.

5. Are there any built-in matrix constructors in C++?

While there are no built-in matrix constructors in C++, there are built-in data structures like arrays and vectors that can be used to create matrices. Additionally, there are also libraries and frameworks that provide matrix classes with pre-defined constructors for convenience.

Similar threads

  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
11
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
Back
Top