Undefined reference to constructor
-
h file class Matrix3 { public: Matrix3(); Matrix3(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal); #include "matrix3.h" #include <QtMath> cpp file Matrix3::Matrix3(){} Matrix3::Matrix3(qreal d11, qreal d12, qreal d13, qreal d21, qreal d22, qreal d23, qreal d31, qreal d32, qreal d33): m11(d11), m12(d12), m13(d13), m21(d21), m22(d22), m23(d23), m31(d31), m32(d32), m33(d33){
-
And do you compile matrix3 source file? Please show your pro-File/CMakeLists.txt
-
@ofmrew
If that is what you have as shown I am surprised it would not complain with'qreal': unknown symbol"
or similar, since as it reads theh file
you show included intocpp file
, no Qt header has been read in yet so I don't see howqreal
would be defined...?Unless it turns out what you are choosing paste is not the whole story of what you actually have, I'm not going to tease that out of you.
I cannot recall whether
int
literal arguments will automatically be promoted todouble
. I suspect they are so that's not the issue, but if not you needMatrix3(2.0, -3.0, 1.0, ...
.Otherwise I'd like to see the actual error message against the actual code, or you can wait for someone else to spot what I can't.
-
-
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++11 # You can make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp \ matrix3.cpp \ mycanvas.cpp \ vector3.cpp HEADERS += \ mainwindow.h \ matrix3.h \ mycanvas.h \ vector3.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target #ifndef MATRIX3_H #define MATRIX3_H #include <QObject> #include <QtMath> class Matrix3 { public: Matrix3(); Matrix3(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal); qreal det(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal); qreal det2(qreal, qreal, qreal, qreal); Matrix3 adj(Matrix3 *); Matrix3 inverse(Matrix3 *); qreal m11; qreal m12; qreal m13; qreal m21; qreal m22; qreal m23; qreal m31; qreal m32; qreal m33; Matrix3 operator/(const qreal&) const; }; #endif // MATRIX3_H #include "matrix3.h" #include <QtMath> Matrix3::Matrix3(){} Matrix3::Matrix3(qreal d11, qreal d12, qreal d13, qreal d21, qreal d22, qreal d23, qreal d31, qreal d32, qreal d33): m11(d11), m12(d12), m13(d13), m21(d21), m22(d22), m23(d23), m31(d31), m32(d32), m33(d33){ } qreal det2(qreal a, qreal b, qreal c, qreal d){ return (a*d) - b*c; } qreal det(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f, qreal g, qreal h, qreal i){ return a*det2(e, f, h, i) -b*det2(d, f, g, i) +c*det2(d, e, g, h); } Matrix3 adj(Matrix3 * m){ qreal am1 = det2(m->m22, m->m23, m->m32, m->m33); qreal am2 = det2(m->m12, m->m13, m->m32, m->m33); qreal am3 = det2(m->m12, m->m13, m->m22, m->m23); qreal am4 = det2(m->m21, m->m23, m->m31, m->m33); qreal am5 = det2(m->m11, m->m13, m->m31, m->m33); qreal am6 = det2(m->m11, m->m13, m->m21, m->m22); qreal am7 = det2(m->m21, m->m22, m->m31, m->m32); qreal am8 = det2(m->m11, m->m22, m->m31, m->m32); qreal am9 = det2(m->m11, m->m12, m->m21, m->m22); Matrix3 am = Matrix3(am1, am2, am3, am4, am5, am6, am7, am8, am9); return am; } Matrix3 Matrix3::operator/(const qreal &d) const{ Matrix3 result; result.m11 =(this->m11/d); result.m12 =(this->m12/d); result.m13 =(this->m13/d); result.m21 =(this->m21/d); result.m22 =(this->m22/d); result.m23 =(this->m23/d); result.m31 =(this->m31/d); result.m32 =(this->m32/d); result.m33 =(this->m33/d); return result; } /*Matrix3 inverse(Matrix3 * m){ Matrix3 minv = adj(m)/det(m); return minv; }*/
It is poor code because I was trying to solve compiler problems. I seems that something has changed i moving from Qt5 to Qt6. Any suggestiond?
-
Remove mainwindow, vector3d and ui file from your pro-file and provide the main.cpp with your calls the matrix3. Also remove all other stuff from matrix3 class except the ctor and the det() function. Then you've a minimal, compilable example where we can take a look on it
-
Sorry but I am confused, I did not anything to the .pro file, that was done by Qt6 Creator. Also there is no ctor.
Everything compiles and the only error is
D:\qt\3DTools\mainwindow.cpp:16: error: undefined reference to `Matrix3::det(double, double, double, double, double, double, double, double, double)' debug/mainwindow.o: In function `MainWindow::MainWindow(QWidget*)': D:\qt\build-3DTools-Desktop_Qt_6_2_0_MinGW_64_bit-Debug/../3DTools/mainwindow.cpp:16: undefined reference to `Matrix3::det(double, double, double, double, double, double, double, double, double)' :-1: error: collect2.exe: error: ld returned 1 exit status :-1: error: [Makefile.Debug:86: debug/3DTools.exe] Error 1
-
@ofmrew
Your.h
file declares a member methodqreal det(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal);
inclass Matrix3
.Your
.cpp
file definesqreal det(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f, qreal g, qreal h, qreal i) { ...
That is a free function, not a member of
Matrix3
.... You also have other free functions defined, and you do not define all member methods.I do not presently see an issue with
Matrix3(2, -3, 1, 3, 0, -1, 1, 4, 5)
as you seem to have defined that constrcutor correctly. But sort out all those free functions and ensure all your declared member methods are defined, and see where you are. -
@ofmrew said in Undefined reference to constructor:
The question is how do I solve the free function problem?
Add the class name before
det()
- c++ basic stuff? -
@ofmrew said in Undefined reference to constructor:
I checked the values at the break point and the looked OK.
No idea what this means, but never mind.
The question is how do I solve the free function problem?
Define them as member functions. If you do not understand the difference between class member functions and free functions you need to understand that from a C++ book/tutorial, else you won't get far in C++.