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

时间:2011-08-31 06:13:13

标签: c++ c linker-errors lnk2019

好吧,我正在为我的类编写这段代码,并且我使用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;
         }
        }

我有什么办法可以解决它吗?

2 个答案:

答案 0 :(得分:7)

您似乎有一个名为PointClass的命名空间,并且使用PointClass保护标头。考虑一开始就改变它。

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

答案 1 :(得分:3)

阅读错误消息:

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

它为您提供了全局变量的名称,或者(更有可能)为没有定义而调用的函数,即您没有为其编写函数体,或者没有将源及其定义添加到项目中。< / p>

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

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

#define PointClass

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

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

#define POINT_H