簡體   English   中英

如何消除程序中的鏈接器錯誤?

[英]How do I eliminate the linker errors in my program?

好吧,我正在為我的課程編寫這段代碼,並且使用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的標頭保護。 考慮從一開始就對其進行更改。

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

閱讀錯誤消息:

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

它為您提供了一個全局變量的名稱,或者為您提供了(更可能是)沒有定義就調用的函數的名稱,即您沒有為其編寫函數體,或者沒有將帶有其定義的源代碼添加到您的項目中。

錯誤LNK1120是第一個錯誤的結果。

沒有完整的錯誤消息,很難分辨。 正如@Darcy所指出的,您應該在#ifdef#define更改名稱,因為

#define PointClass

告訴預處理器在以下源代碼中將此名稱替換為任何名稱。 這將導致未命名的本地名稱空間。 在此本地名稱空間內定義的所有名稱只能從該編譯單元(cpp文件)內部訪問。

為“包含防護”選擇一個唯一的名稱,該名稱不應出現在程序的其他任何位置。 可能的模式可以模仿頭文件的名稱:

#define POINT_H

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM