示例程序中的 C++ 链接错误

c++ linking error in example program

我正在尝试一些 C++,作为对 类 的介绍,我尝试用点 (punt.cpp) 编写一个三角形 (driehoek.cpp)。现在,我的 main 什么都不做,但我收到以下链接错误:

    Undefined symbols for architecture x86_64:
  "punt::punt()", referenced from:
      driehoek::driehoek(punt&, punt&, punt&) in driehoek.o
      ___cxx_global_var_init in driehoek.o
      ___cxx_global_var_init1 in driehoek.o
      ___cxx_global_var_init2 in driehoek.o
ld: symbol(s) not found for architecture x86_64

这是我用于该项目的文件:

driehoek.h:

#ifndef DRIEHOEK_H
#define DRIEHOEK_H
#include "punt.h"


class driehoek {
public:
    driehoek();
    driehoek(punt &a, punt &b, punt &c);
    driehoek(const driehoek& orig);
    virtual ~driehoek();
    void setA(punt &a);
    void setB(punt &b);
    void setC(punt &c);
    void print();
    punt getA();
    punt getB();
    punt getC();
private:
    punt a;
    punt b;
    punt c;
};

#endif  /* DRIEHOEK_H */

driehoek.cpp:

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


    punt a;
    punt b;
    punt c;

driehoek::driehoek(punt::punt &a, punt::punt &b, punt::punt &c) {
        this->a = a;
        this->b = b;
        this->c = c;
    }

    driehoek::~driehoek() {
        delete this;
    }

void setA(punt &pu){
    a = pu;
}

void setB(punt &pu){
    b = pu;
}

void setC(punt &pu){
    c = pu;
}

void driehoek::print(){
    std::cout << "pA = " << &a << " pB=" << &b << " pC";
}

punt.h:

#ifndef PUNT_H
#define PUNT_H

class punt {
public:
    punt();
    punt(int x, int y);
    punt(const punt& orig);
    virtual ~punt();
    void setX(int x);
    void setY(int y);
    int getX();
    int getY();
    float distance(punt pu);
private:
    int x;
    int y;
};

#endif  /* PUNT_H */

punt.cpp:

#include <math.h>
#include "punt.h"
    int x;
    int y;

    punt::punt(int x, int y) {
        this->x = x;
        this->y = y;
    }

    punt::~punt() {
        delete &y;
        delete &x;
        delete this;
    }

    void punt::setX(int x){
        this->x = x;
    }

    void punt::setY(int y) {
        this->y = y;
    }

    int punt::getX() {
        return this->x;
    }

    int punt::getY() {
        return this->y;
    }

float punt::distance(punt pu){

    return
    sqrt(
    ((this->x - pu.x) * (this->x - pu.x))
    +
    ((this->y - pu.y) * (this->y - pu.y))

    );
}

为了完整起见,main.cpp:

#include <cstdlib>

using namespace std;

    /*
     * 
     */
    int main(int argc, char** argv) {

        return 0;
    }

我确信同样的问题已经被其他人问过,但是在搜索了一段时间后我找不到任何我理解的答案。如果结果是重复的,我深表歉意。

需要默认构造函数 punt::punt() 来创建您在 "driehoek.cpp" 中添加的三个非成员变量。
您收到相关错误是因为您声明了该构造函数但从未定义它。

("punt.cpp" 中也有一些不必要的变量。)

如下更改构造函数定义,

driehoek::driehoek(punt &a, punt &b, punt &c) {
        this->a = a;
        this->b = b;
        this->c = c;
    }

声明的构造函数及其定义与您的 class "driehoek" 不匹配。