Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Undefined reference to constructor

Undefined reference to constructor

Scheduled Pinned Locked Moved Unsolved Qt 6
13 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #1

    I am only using my c++ code, no libraries.

        Matrix3 * m = new Matrix3(2, -3, 1, 3, 0, -1, 1, 4, 5);
        Matrix3 mx;
        qreal a = mx.det(2, -3, 1, 3, 0, -1, 1, 4, 5);
    

    If I use m->det(... I have the same problem. What is wrong? Thanks.

    JonBJ 1 Reply Last reply
    0
    • O ofmrew

      I am only using my c++ code, no libraries.

          Matrix3 * m = new Matrix3(2, -3, 1, 3, 0, -1, 1, 4, 5);
          Matrix3 mx;
          qreal a = mx.det(2, -3, 1, 3, 0, -1, 1, 4, 5);
      

      If I use m->det(... I have the same problem. What is wrong? Thanks.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @ofmrew said in Undefined reference to constructor:

      I am only using my c++ code, no libraries.

      No libraries? Where do you and your compiler get something called Matrix from then, because it's not part of C++?

      1 Reply Last reply
      0
      • O Offline
        O Offline
        ofmrew
        wrote on last edited by
        #3
        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){
        
        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          And do you compile matrix3 source file? Please show your pro-File/CMakeLists.txt

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • O ofmrew
            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){
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @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 the h file you show included into cpp file, no Qt header has been read in yet so I don't see how qreal 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 to double. I suspect they are so that's not the issue, but if not you need Matrix3(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.

            Christian EhrlicherC 1 Reply Last reply
            0
            • JonBJ JonB

              @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 the h file you show included into cpp file, no Qt header has been read in yet so I don't see how qreal 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 to double. I suspect they are so that's not the issue, but if not you need Matrix3(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.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @JonB There is also no det() function in the header nor source file.

              @ofmrew sow the whole code to reproduce your issue incl. the pro-file/CMakeLists.txt

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1
              • O Offline
                O Offline
                ofmrew
                wrote on last edited by
                #7
                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?

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1
                  • O Offline
                    O Offline
                    ofmrew
                    wrote on last edited by
                    #9

                    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
                    
                    JonBJ 1 Reply Last reply
                    0
                    • O ofmrew

                      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
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @ofmrew
                      Your .h file declares a member method qreal det(qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal, qreal); in class Matrix3.

                      Your .cpp file defines

                      qreal 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.

                      1 Reply Last reply
                      2
                      • O Offline
                        O Offline
                        ofmrew
                        wrote on last edited by
                        #11

                        I checked the values at the break point and the looked OK. The question is how do I solve the free function problem?

                        Christian EhrlicherC JonBJ 2 Replies Last reply
                        0
                        • O ofmrew

                          I checked the values at the break point and the looked OK. The question is how do I solve the free function problem?

                          Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @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?

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          1 Reply Last reply
                          0
                          • O ofmrew

                            I checked the values at the break point and the looked OK. The question is how do I solve the free function problem?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @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++.

                            1 Reply Last reply
                            2

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved