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

发布于 2024-12-02 03:25:50 字数 3973 浏览 2 评论 0原文

好吧,我正在为我的类编写这段代码,但使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇妻 2024-12-09 03:25:50

您似乎有一个名为 PointClass 的命名空间,并且使用 PointClass 进行标头保护。首先考虑改变这一点。

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

You seem to have a namespace called PointClass, and headers guards using PointClass. Consider changing that for a start.

#ifndef PointClass // Rename this, it might be clashing with your namespace. 
#define PointClass
namespace PointClass
甜妞爱困 2024-12-09 03:25:50

阅读错误消息:

Linker Error 2019: unresolved external symbol 'symbol' referenced in function 'function'

它为您提供了全局变量的名称或(更有可能)在没有定义的情况下调用的函数,即您没有为其编写函数体,或者没有将带有其定义的源添加到您的项目中。

错误 LNK1120 是第一个错误的结果。

如果没有完整的错误消息,很难判断。正如 @Darcy 指出的,您应该更改 #ifdef#define 中的名称,因为

#define PointClass

告诉预处理器在以下源代码。这会产生一个未命名的本地命名空间。只能从该编译单元(cpp 文件)内部访问此本地命名空间内定义的所有名称。

为“包含防护”选择一个唯一的名称,该名称不应出现在程序的其他任何位置。可能的模式可以模仿头文件的名称:

#define POINT_H

Read the error message:

Linker Error 2019: unresolved external symbol 'symbol' referenced in function 'function'

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 #ifdefand #define since

#define PointClass

tells 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:

#define POINT_H
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文