c++ - 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
解决方案
推荐阅读
- android - React-Native:如何在 Android 中访问 react-native 应用程序的权限?
- arrays - VBA Excel:如何通过函数将值分配给数组中的空槽
- android - 可关闭小部件的边框拖动端
- java - 用嵌入式数据库替换真实数据源进行测试
- cors - 当我尝试使用 Fetch from React 进行 POST 时,Cors 阻止了错误 403
- elixir - 使用 Phoenix 应用程序监控文件系统(文件创建)
- vue.js - 无法使用动态 Vuex 存储数据填充 VueJS 日历插件的数据数组
- mysql - 存储过程在 localhost 中有效,但在服务器中无效
- swift - 尝试在 SwiftUI 中的 UIViewRepresentable 中将 HTML 转换为 AttributedString
- php - 无法为命名路由“参与者”生成 URL,因为此类路由不存在