gpt4 book ai didi

c++ - 如何消除程序中的链接器错误?

转载 作者:太空宇宙 更新时间:2023-11-04 05:11:03 26 4
gpt4 key购买 nike

嗯,我正在为我的类(class)编写这段代码,但我在使用 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;
}
}

有什么办法可以解决吗?

最佳答案

您似乎有一个名为 PointClass 的命名空间,以及使用 PointClass 的 header 守卫。考虑先改变它。

#ifndef PointClass // Rename this, it might be clashing with your namespace. 
#define PointClass
namespace PointClass

关于c++ - 如何消除程序中的链接器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7253211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com