首页 > 解决方案 > c ++链接器命令错误(体系结构的未定义符号)

问题描述

我在头文件“Point.hpp”中创建了一个类,并想在源文件“main.cpp”中执行 main() 函数,但我不断收到链接器错误,如下所示:

架构 x86_64 的未定义符号:“Point::ToString()”,引用自:main-c832f3.o 中的 _main “Point::Point()”,引用自:main-c832f3.o 中的 _main “Point::~Point ()”,引用自:main-c832f3.old 中的 _main:未找到架构 x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)

// Point.hpp
#ifndef POINT_HPP   // if header not defined

#define POINT_HPP   // define header
#include <iostream> // needed for std::cout, std::endl
#include <string>   // needed for std::string

class Point
{
    // not accessible to users
    private:
        // data members
        double m_x; // declare x point with type double
        double m_y; // declare y point with type double

    // accessible to users
    public: // public functionality
        // default constructor and destructor
        Point();
        ~Point();

        // getter methods
        double GetX() const {return m_x;};   // const get() m_x method
        double GetY() const {return m_y;};   // const get() m_y method

        // setter methods
        void SetX(double x) {m_x = x;}; // set(x) function with user input
        void SetY(double y) {m_y = y;}; // set(y) function with user input

        // string of the Point (x,y)
        std::string ToString()
           {std::stringstream string;   // string is stream buffer
            string << "\nPoint(" << Point::m_x << "," << Point::m_y << ")\n"; // store variables in string buffer
            return string.str();    // return string output using .str()};  // std output Point(x,y) in string
           };

#endif  // end conditional compilation


// Test.cpp
/* this source file tests the class defined in Point.hpp */

#include <iomanip>  // needed for std::setprecision(), input is # of decimal places
#include "Point.hpp"    // point header file

int main()
{
    double x, y;    // declare user input variables, type double

    /* asking user for x- and y- coordinates using std::cout and std::cin */
    std::cout << "Enter a value for x:\n" << std::endl;
    std::cin >> x;
    std::cout << "Enter a value for y:\n" << std::endl;
    std::cin >> y;

    Point point;    // constructing point object
    point.SetX(x);  // set user input x-coordinate
    point.SetY(y);  // set user input y-coordinate

    std::cout << point.ToString() << std::endl; // print ToString method
    std::cout << point.GetX() << std::endl; // print GetX
    std::cout << point.GetY() << std::endl; // print GetY

标签: c++

解决方案


推荐阅读