LNK1120: 1 unresolved externals and LNK2019: unresolved external symbol

前端 未结 3 521
北海茫月
北海茫月 2021-01-23 12:56

I\'ve been getting these two errors and I cant seem to find a solution that works.

LNK1120: 1 unresolved externals

Error 1 error LNK2019: unresolve

相关标签:
3条回答
  • 2021-01-23 13:05

    Your implementation of Vector3D appears to be missing an actual implementation for the copy constructor, hence the unresolved external error. If you don't intend the Vector3D object to be copied, you can't pass it into Matrix::operator* by value as that would trigger a copy.

    That said, I don't think there is any reason to declare and implement the copy constructor for Vector3D anyway as it only contains POD types and the compiler generated copy constructor will work fine. The same goes for the destructor, there are no resources to manage so let the compiler do its job.

    0 讨论(0)
  • 2021-01-23 13:06

    It says that the linker cannot find an implementation of Vector3D(const Vector3D& rhs);. This constructor is declared in your vector class, but not defined.

    Do you have an implementation of the constructor somewhere in a .cpp file, and is this file known to your compiler?

    C/C++ compilation works like that: At first, you have a number of so called "compilation units" - normally, every .cpp-file is one such compilation unit. Your program consists of all these separate units linked together (the "linking" process, happens after compilation). Every function that is called somewhere has to be defined exactly once in some compilation unit, unless it is defined inline (like the other methods of your class). If a method is declared, but not defined, the compiler will not complain - only the linker will. Imagine the compilation units having "sockets" and "connectors" which fit to the corresponding "sockets" of other units. The compilation process just creates these units assuming a particular "socket" shape (as given by declarations), whereas the linker actually tries to connect each "socket" with it's "connector". So you see how the compilation process may suceed, but the linking not.

    Linker errors can be tricky to solve, especially if you're not that experienced yet. There can be many causes for them:

    • Missing implementation/definition
    • Definition exists, but somehow is not compiled (because the file is not passed to the compiler etc.)
    • Different versions of libraries etc.

    And many more..

    Edit: Apart from that, you should pass the vector by const reference, and create newVector by invoking it's constructor, instead of creating a default constructed object and then assigning. And the final construction in the return statement is not needed as well. Improved code:

    Vector3D Matrix4::operator *(const Vector3D& vector)
    {
        return Vector3D(
            (m[0][0] * vector.GetVector_X()) + (m[0][1] * vector.GetVector_Y()) + (m[0][2] * vector.GetVector_Z()) + m[0][3],
            (m[0][0] * vector.GetVector_X()) + (m[1][1] * vector.GetVector_Y()) + (m[1][2] * vector.GetVector_Z()) + m[1][3],
            (m[0][0] * vector.GetVector_X()) + (m[2][1] * vector.GetVector_Y()) + (m[2][2] * vector.GetVector_Z()) + m[2][3]
        );
    }
    
    0 讨论(0)
  • 2021-01-23 13:06

    Did you implement the Vector3D default constructor, copy constructor, and destructor? You showed your headers but not the implementation files. The linker complains about a missing defintion of Vector3D::Vector3D(Vector3D const&).

    0 讨论(0)
提交回复
热议问题