C++ "undefined reference to class::function"

标签 c++ undefined-reference

到目前为止,我已经设法修复了自己的错误,但是这个错误让我困惑了一段时间。如果你们有任何提示,我们将不胜感激!谢谢。

我在 Windows 笔记本电脑上使用 Eclipse,构建后出现以下错误: 对“Point::Point()”circle.cpp 的 undefined reference /Assignment4_1/IN4084MSc 第 13 行 C/C++ 问题

我的所有文件(main4_1.cpp、point.h、point.cpp、circle.h、circle.cpp)都是项目(称为Assignment4_1)的一部分。

#include "point.h"
#include "circle.h"
#include <cmath>
using namespace std;

Circle::Circle(Point ct, double rd):Point(ct)
{
    center = ct;
    if(rd > 0)
        {
        radius = rd;
        }
    radius = -1;
}

Point Circle::get_center(){
    return center;
}

double Circle::get_radius(){
    return radius;
}

void Circle::print(){
    cout << " <Circle(<Point(“"<<center.get_x()<<"”,“"<<center.get_y()<<"”)>,”"<<radius<<"”)>";
}

void Circle::print(string s){
    cout << s;
    print();
}

void Circle::println(){
    print();
    cout << endl;
}

void Circle::println(string s){
    print(s);
    cout << endl;
}




#ifndef CIRCLE_H_
#define CIRCLE_H_

#include <iostream>
#include "point.h"

using namespace std;

class Circle: public Point{
    Point center;
    double radius;

public:
    Circle(Point ct, double rd);
    Point get_center();
    double get_radius();
    void print();
    void print(string s);
    void println();
    void println(string s);

};

#endif /* CIRCLE_H_ */



#include "point.h"
#include <cmath>
using namespace std;

Point::Point(double x, double y){
        x_coord = x;
        y_coord = y;
}

double Point::get_x(){
    return x_coord;
}

double Point::get_y(){
    return y_coord;
}

void Point::print(){
//post: has printed the contents of the Point object in the format
    cout << "<Point( “ " << x_coord <<" ” , “ " << y_coord << " ” )>";
}


void Point::print(string s){
//post: has printed string s first, then printed the contents of the Point object
    cout << s << " ";
    print();
}

void Point::println(){
//post: has printed the contents of Point object, then moved the cursor to the next line
    print();
    cout << endl;
}

void Point::println(string s){
//post: has printed string s first, then printed the contents of the Point object, and moved the cursor to the next line
    cout << s << " ";
    println();
}

void Point::move(double dx, double dy){
// post: x_coord = x_coord + dx, y_coord = y_coord + dy
    x_coord = x_coord + dx;
    y_coord = y_coord + dy;
}

double Point::distance(Point that){
//post: returns the distance between this Point and that Point
    return sqrt( pow(x_coord - that.get_x(),2) + pow(y_coord - that.get_y(),2) );
}

bool Point::equals(Point that){
//post : returns true if this Point and that Point have the same coordinates
    return (x_coord = that.get_x());
}


#ifndef POINT_H_
#define POINT_H_

#include <iostream>

using namespace std;

class Point{

protected:
    double x_coord;
    double y_coord;

public:
    Point(double x, double y);
    Point();
    double get_x();
    double get_y();
    void print();
    void print(string s);
    void println();
    void println(string s);
    void move(double dx, double dy);
    double distance(Point that);
    bool equals(Point that);

};

#endif /* POINT_H_ */


#include <iostream>
#include <cstdlib>
using namespace std;

#include "point.h"
#include "circle.h"

void test1(){
    cout << "test1:" << endl;

    Point p1 = Point(2,3);
    cout << "Point p1 is created, the data of p1 = (2,3)" << endl;
    cout << "p1.get_x() -> " << p1.get_x() << endl;
    cout << "p1.get_y() -> " << p1.get_y() << endl << endl;

    p1.println("p1.println() -> ");
    cout << endl;

    cout << "end test1" << endl << endl;
}

void test2(){
    cout << "test2:" << endl;

    cout << "Point p1 is created, the data of p1 = (2,3)" << endl;
    cout << "Point p2 is created, the data of p2 = (0,0)" << endl;
    Point p1 = Point(2,3);p1.println("p1.println() -> ");
    Point p2 = Point(0,0);p2.println("p2.println() -> ");
    cout << endl;

    p1.println("p1 before move -> ");
    cout << "p1.move(1,1)" << endl;p1.move(1,1);
    p1.println("p1 after move -> ");
    cout << endl;

    cout << "p1.distance(p2) -> " << p1.distance(p2) << endl << endl;
    cout << "p1.equals(p1)   -> " << p1.equals(p1) << endl;
    cout << "p1.equals(p2)   -> " << p1.equals(p2) << endl;
    cout << "end test2" << endl << endl;
}

int main(){

    test1();
    test2();

    return 0;
}

最佳答案

编译器告诉你定义

Point::Point()

您仅在 Point 类定义中声明了它。一种可能的实现是用 0.0 初始化两个坐标:

Point::Point() : x_coord(0.0), y_coord(0.0) {}

在这种情况下,您必须显式初始化成员,因为内置类型在默认构造时不会进行零初始化。

关于C++ "undefined reference to class::function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19450080/

相关文章:

c++ - 没有编写 std::for_each 来获取 std::list 是有原因的吗?

c++ - 使用 openssl 封装数据包

c++ - 为字符串参数重载 sqrt 函数

c++ - 带有头文件的纯函数文件总是导致 undefined reference

c++ - 链接时突然收到 maxrregcount 警告和 undefined reference 错误

c++ - 异常处理期间的类型解析

c++ - 在链接阶段诊断 "contains undefined reference for architecture"

c++ - 什么是 undefined reference /未解析的外部符号错误,我该如何解决?

c++ - 为什么模板只能在头文件中实现?

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?