LNK1120:1个未解析的外部元件,LNK2019:未解析的外部符号

编程入门 行业动态 更新时间:2024-10-25 22:31:28
本文介绍了LNK1120:1个未解析的外部元件,LNK2019:未解析的外部符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直遇到这两个错误,我似乎找不到有效的解决方案.

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

LNK1120:1个未解决的外部

LNK1120: 1 unresolved externals

错误1错误LNK2019:未解决的外部符号"public:__thiscall Vector3D :: Vector3D(Vector3D const&)类"(?? 0Vector3D @@ QAE @ ABV0 @@ Z)在函数"public:class Vector3D __thiscall Vertex"中引用:: GetPosition(void)"(?GetPosition @ Vertex @@ QAE?AVVector3D @@ XZ)

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Vector3D::Vector3D(class Vector3D const &)" (??0Vector3D@@QAE@ABV0@@Z) referenced in function "public: class Vector3D __thiscall Vertex::GetPosition(void)" (?GetPosition@Vertex@@QAE?AVVector3D@@XZ)

我认为这与我的Vector 3d类中的Matrix运算符和构造函数有关 任何帮助将不胜感激,因为我是C ++的新手

I think its to do with my Matrix operator and the constructor in my Vector 3d class Any help will be much appreciated as I am quite new to C++

#ifndef MATRIX4_H #define MATRIX4_H #include "Vector3D.h" class Matrix4 { public: Matrix4(); Matrix4(const Matrix4& rhs); ~Matrix4(); Vector3D Matrix4::operator *(Vector3D vector) { Vector3D newVector; newVector.SetVector_X((m[0][0] * vector.GetVector_X()) + (m[0][1] * vector.GetVector_Y()) + (m[0][2] * vector.GetVector_Z()) + m[0][3]); newVector.SetVector_Y((m[0][0] * vector.GetVector_X()) + (m[1][1] * vector.GetVector_Y()) + (m[1][2] * vector.GetVector_Z()) + m[1][3]); newVector.SetVector_Z((m[0][0] * vector.GetVector_X()) + (m[2][1] * vector.GetVector_Y()) + (m[2][2] * vector.GetVector_Z()) + m[2][3]); return Vector3D(newVector.GetVector_X(),newVector.GetVector_Y(),newVector.GetVector_Z()); } void SetMatrix(float matrix[4][4]) { memcpy(m,matrix,sizeof(matrix)); } private: float m[4][4]; }; #endif

Vector3D.h文件

Vector3D.h file

#ifndef VECTOR3D_H #define VECTOR3D_H class Vector3D { public: Vector3D(); Vector3D(const Vector3D& rhs); ~Vector3D(); Vector3D(float VectorX, float VectorY, float VectorZ) { x=VectorX; y=VectorY; z=VectorZ; } void SetVector3D(float vector_X, float vector_Y, float vector_Z) { x = vector_X; y = vector_Y; z = vector_Z; } void SetVector_X(float vector_X) { x=vector_X; } void SetVector_Y(float vector_Y) { y=vector_Y; } void SetVector_Z(float vector_Z) { z=vector_Z; } float GetVector_X() { return x; } float GetVector_Y() { return y; } float GetVector_Z() { return z; } Vector3D GetVector() { return Vector3D(x,y,z); } private: float x; float y; float z; }; #endif

推荐答案

它表示链接程序找不到Vector3D(const Vector3D& rhs);的实现.此构造函数在向量类中声明,但未定义.

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.

您是否在.cpp文件中的某处实现了构造函数的实现,并且编译器是否知道此文件?

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

C/C ++编译的工作方式如下:首先,您有许多所谓的编译单元"-通常,每个.cpp文件都是一个这样的编译单元.您的程序由链接在一起的所有这些单独的单元组成(链接"过程在编译后发生).必须在某个编译单元中定义一次,每个在某个地方调用的函数都必须被定义一次,除非是内联定义的(就像您的类的其他方法一样).如果已声明但未定义方法,则编译器不会抱怨-仅链接器会抱怨.想象一下具有插槽"和连接器"的编译单元,它们适合于其他单元的相应插槽".编译过程只是按照特定的套接字"形状(由声明指定)创建这些单元,而链接器实际上是尝试将每个套接字"与其连接器"连接起来.因此,您将看到编译过程是如何实现的,但链接没有实现.

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:

  • 缺少实现/定义
  • 存在定义,但由于某种原因未将文件传递给编译器等原因,因此未进行编译
  • 不同版本的库等.

还有更多..

除此之外,还应该通过const引用传递矢量,并通过调用其构造函数来创建newVector,而不是创建默认的构造对象然后进行分配.并且return statement中的最终构造也不是必需的.改进的代码:

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] ); }

更多推荐

LNK1120:1个未解析的外部元件,LNK2019:未解析的外部符号

本文发布于:2023-05-27 17:41:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/297756.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元件   符号

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!