0

I have a physics Vector class which requires a constructor and to string function. When I compile the class I get the following error:

I invoke the compiler like so: g++ main.cpp -o main

Undefined symbols for architecture x86_64:
  "Vector::toString()", referenced from:
      _main in main-3acaf8.o
  "Vector::Vector()", referenced from:
      _main in main-3acaf8.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Which implies I have not defined the constructor and toString function. Here is my code, how do I resolve this?

Header file:

#include <iostream>
#include <cmath>

class Vector {

    const static int d = 3, xPos = 0, yPos = 1, zPos = 2;
    double components[d];

    public:
        Vector();
        Vector(double x, double y, double z);
        Vector(const Vector &v1);
        void setVector(double x, double y, double z);
        double getX();
        double getY();
        double getZ();
        double magnitude();
        Vector getUnitVector();
        double dot(Vector v1, Vector v2);
        void scale(double c);
        Vector add(Vector v1, Vector v2);
        Vector subtract(Vector v1, Vector v2);
        Vector crossProduct(Vector v1, Vector v2);
        void toString();
};

cpp file:

#include "vector.hpp"

Vector::Vector(){
    for(int i = 0; i < d; i++)
        components[i] = 0;

... // all other functions

void Vector::toString(){
    printf("x: %lf, y: %lf, z: %lf\n", 
        components[0], components[1], components[2]);
}

Thank you.

Tom Finet
  • 2,056
  • 6
  • 30
  • 54

0 Answers0