gorgabal
gorgabal

Reputation: 145

c++ linking error in example program

I am trying out some c++, and as an introduction to classes, I tried to program a triangle(driehoek.cpp) with points(punt.cpp). For now, my main does nothing, but I am getting the following linking error:

    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

here are the files i use for this project:

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))

    );
}

And for completeness, main.cpp:

#include <cstdlib>

using namespace std;

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

        return 0;
    }

I am sure that the same question has already be asked by someone else, but after searching for a while I couldn't find any answers that I understood. My apologies if this turns out to be a duplicate.

Upvotes: 1

Views: 159

Answers (2)

Change your constructor definition as like as below,

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

The declared constructor and its definition doesn't match for your class "driehoek" .

Upvotes: 0

molbdnilo
molbdnilo

Reputation: 66459

The default constructor, punt::punt(), is needed for creating the three non-member variables you added in "driehoek.cpp".
You're getting the error in question because you declared that constructor but you never defined it.

(There are a couple of unnecessary variables in "punt.cpp", too.)

Upvotes: 2

Related Questions