如何消除程序中的链接器错误?
好吧,我正在为我的类编写这段代码,但使用 Visual Studio 时遇到链接器错误 LNK2019 和 LNK1120。我不太清楚它为什么要做它正在做的事情,但我离题了。
头文件:
#ifndef PointClass
#define PointClass
#include <iostream>
namespace PointClass
{
class point
{
public:
// CONSTRUCTOR
point(double initial_x = 0.0, double initial_y = 0.0);
// MODIFICATION MEMBER FUNCTIONS
void shift(double x_amount, double y_amount);
void rotate90( );
// CONSTANT MEMBER FUNCTIONS
double get_x( ) const { return x; }
double get_y( ) const { return y; }
// FRIEND FUNCTION
friend std::istream& operator >>(std::istream& ins, point& target);
double x, y; // x and y coordinates of this point
};
// NONMEMBER FUNCTIONS for the point class
double distance(const point& p1, const point& p2);
point middle(const point& p1, const point& p2);
point operator +(const point& p1, const point& p2);
bool operator ==(const point& p1, const point& p2);
bool operator !=(const point& p1, const point& p2);
std::ostream& operator <<(std::ostream & outs, const point& source);
}
#endif
和 CPP 文件:
#include <iostream>
#include <math.h>
#include "point.h"
using namespace std;
namespace PointClass
{
point::point(double initial_x, double initial_y)
{
x = initial_x; // Constructor sets point to a given position
y = initial_y;
}
void point::shift(double x_amount, double y_amount)
{
x += x_amount;
y += y_amount;
}
void point::rotate90( )
{
double new_x;
double new_y;
new_x = y; // For a 90 degree clockwise rotation the new y is -1
new_y = -x; // times original x, and the new x is the original y
x = new_x;
y = new_y;
}
bool operator ==(const point& p1, const point& p2)
{
return
(p1.get_x( ) == p2.get_x( ))
&&
(p1.get_y( ) == p2.get_y( ));
}
bool operator !=(const point& p1, const point& p2)
{
return !(p1 == p2);
}
point operator +(const point& p1, const point& p2)
{
double x_sum, y_sum;
// Compute the x and y of the sum:
x_sum = (p1.get_x( ) + p2.get_x( ));
y_sum = (p1.get_y( ) + p2.get_y( ));
point sum(x_sum, y_sum);
return sum;
}
ostream& operator <<(ostream& outs, const point& source)
{
outs << source.get_x( ) << " " << source.get_y( );
return outs;
}
istream& operator >>(istream& ins, point& target)
{
ins >> target.x >> target.y;
return ins;
}
int rotations_needed(point p)
{
int answer;
answer = 0;
while ((p.get_x( ) < 0) || (p.get_y( ) < 0))
{
p.rotate90( );
++answer;
}
return answer;
}
void rotate_to_upper_right(point& p)
{
while ((p.get_x( ) < 0) || (p.get_y( ) < 0))
p.rotate90( );
}
double distance(const point& p1, const point& p2)
{
double a, b, c_squared;
// Calculate differences in x and y coordinates
a = p1.get_x( ) - p2.get_x( ); // Difference in x coordinates
b = p1.get_y( ) - p2.get_y( ); // Difference in y coordinates
// Pythagorean Theorem to calculate square of distance between points
c_squared = a*a + b*b;
return sqrt(c_squared); // sqrt calculates square root
}
point middle(const point& p1, const point& p2)
{
double x_midpoint, y_midpoint;
// Compute the x and y midpoints
x_midpoint = (p1.get_x( ) + p2.get_x( )) / 2;
y_midpoint = (p1.get_y( ) + p2.get_y( )) / 2;
// Construct a new point and return it
point midpoint(x_midpoint, y_midpoint);
return midpoint;
}
}
我可以做些什么来修复它吗?
Well, I'm writing this code for my class and I'm getting linker errors LNK2019 and LNK1120 using Visual Studio. I'm not all too sure why it's doing what it's doing, but I digress.
Header file:
#ifndef PointClass
#define PointClass
#include <iostream>
namespace PointClass
{
class point
{
public:
// CONSTRUCTOR
point(double initial_x = 0.0, double initial_y = 0.0);
// MODIFICATION MEMBER FUNCTIONS
void shift(double x_amount, double y_amount);
void rotate90( );
// CONSTANT MEMBER FUNCTIONS
double get_x( ) const { return x; }
double get_y( ) const { return y; }
// FRIEND FUNCTION
friend std::istream& operator >>(std::istream& ins, point& target);
double x, y; // x and y coordinates of this point
};
// NONMEMBER FUNCTIONS for the point class
double distance(const point& p1, const point& p2);
point middle(const point& p1, const point& p2);
point operator +(const point& p1, const point& p2);
bool operator ==(const point& p1, const point& p2);
bool operator !=(const point& p1, const point& p2);
std::ostream& operator <<(std::ostream & outs, const point& source);
}
#endif
And the CPP file:
#include <iostream>
#include <math.h>
#include "point.h"
using namespace std;
namespace PointClass
{
point::point(double initial_x, double initial_y)
{
x = initial_x; // Constructor sets point to a given position
y = initial_y;
}
void point::shift(double x_amount, double y_amount)
{
x += x_amount;
y += y_amount;
}
void point::rotate90( )
{
double new_x;
double new_y;
new_x = y; // For a 90 degree clockwise rotation the new y is -1
new_y = -x; // times original x, and the new x is the original y
x = new_x;
y = new_y;
}
bool operator ==(const point& p1, const point& p2)
{
return
(p1.get_x( ) == p2.get_x( ))
&&
(p1.get_y( ) == p2.get_y( ));
}
bool operator !=(const point& p1, const point& p2)
{
return !(p1 == p2);
}
point operator +(const point& p1, const point& p2)
{
double x_sum, y_sum;
// Compute the x and y of the sum:
x_sum = (p1.get_x( ) + p2.get_x( ));
y_sum = (p1.get_y( ) + p2.get_y( ));
point sum(x_sum, y_sum);
return sum;
}
ostream& operator <<(ostream& outs, const point& source)
{
outs << source.get_x( ) << " " << source.get_y( );
return outs;
}
istream& operator >>(istream& ins, point& target)
{
ins >> target.x >> target.y;
return ins;
}
int rotations_needed(point p)
{
int answer;
answer = 0;
while ((p.get_x( ) < 0) || (p.get_y( ) < 0))
{
p.rotate90( );
++answer;
}
return answer;
}
void rotate_to_upper_right(point& p)
{
while ((p.get_x( ) < 0) || (p.get_y( ) < 0))
p.rotate90( );
}
double distance(const point& p1, const point& p2)
{
double a, b, c_squared;
// Calculate differences in x and y coordinates
a = p1.get_x( ) - p2.get_x( ); // Difference in x coordinates
b = p1.get_y( ) - p2.get_y( ); // Difference in y coordinates
// Pythagorean Theorem to calculate square of distance between points
c_squared = a*a + b*b;
return sqrt(c_squared); // sqrt calculates square root
}
point middle(const point& p1, const point& p2)
{
double x_midpoint, y_midpoint;
// Compute the x and y midpoints
x_midpoint = (p1.get_x( ) + p2.get_x( )) / 2;
y_midpoint = (p1.get_y( ) + p2.get_y( )) / 2;
// Construct a new point and return it
point midpoint(x_midpoint, y_midpoint);
return midpoint;
}
}
Is there anything I can do to fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎有一个名为 PointClass 的命名空间,并且使用 PointClass 进行标头保护。首先考虑改变这一点。
You seem to have a namespace called PointClass, and headers guards using PointClass. Consider changing that for a start.
阅读错误消息:
它为您提供了全局变量的名称或(更有可能)在没有定义的情况下调用的函数,即您没有为其编写函数体,或者没有将带有其定义的源添加到您的项目中。
错误 LNK1120 是第一个错误的结果。
如果没有完整的错误消息,很难判断。正如 @Darcy 指出的,您应该更改
#ifdef
和#define
中的名称,因为告诉预处理器在以下源代码。这会产生一个未命名的本地命名空间。只能从该编译单元(cpp 文件)内部访问此本地命名空间内定义的所有名称。
为“包含防护”选择一个唯一的名称,该名称不应出现在程序的其他任何位置。可能的模式可以模仿头文件的名称:
Read the error message:
It gives you the name of a global variable or (more likely) a function that is called without definition, i.e. you didn't write a function body for it or did not add the source with its definition to your project.
The error LNK1120 is a consequence of the first error.
Without the complete error message it's hard to tell. As @Darcy pointed out, you should change the names in
#ifdef
and#define
sincetells the preprocessor to substitute this name with nothing in the following source code. This results in a unnamed local namespace. All names defined inside this local namespace are accessible from inside that compilation unit (cpp file) only.
Choose a unique name for the "include guard" which should not occure anywhere else in the program. A possible pattern could imitate the name of the header file: