G ++ Undefined Symbols for x86_64 Architecture

I am learning C ++ and got the task to create a Vector3D class. When I try to compile main.cpp using g ++ on OSX, I get the following error message. Why should it be?

g++ main.cpp 
Undefined symbols for architecture x86_64:
  "Vector3DStack::Vector3DStack(double, double, double)", referenced from:
      _main in cc9dsPbh.o
ld: symbol(s) not found for architecture x86_64

main.cpp

#include <iostream>;
#include "Vector3DStack.h";

using namespace std;

int main() {
    double x, y, z;
    x = 1.0, y = 2.0, z = 3.0;
    Vector3DStack v (x, y, z);
    return 0;
}

Vector3DStack.h

class Vector3DStack {
public:
    Vector3DStack (double, double, double);

    double getX ();
    double getY ();
    double getZ ();

    double getMagnitude();

protected:
    double x, y, z;
};

Vector3DStack.cpp

#include <math.h>;
#include "Vector3DStack.h";

Vector3DStack::Vector3DStack (double a, double b, double c) {
    x = a;
    y = b;
    z = c;
}

double Vector3DStack::getX () {
    return x;
}

double Vector3DStack::getY () {
    return y;
}

double Vector3DStack::getZ () {
    return z;
}

double Vector3DStack::getMangitude () {
    return sqrt (pow (x, 2) * pow (y, 2) * pow (z, 2));
}
+4
source share
3 answers

You must compile and link yours Vector3DStack.cpp. Try:

g++ main.cpp Vector3DStack.cpp -o vectortest

This should create an executable file called vectortest.

+18
source

Pass the implementation to the compiler Vector3D:

g++ main.cpp Vector3DStack.cpp

This will create an executable called a.outon Linux and Unix systems. To change the name of the executable file, use the parameter -o:

g++ -o my_program main.cpp Vector3DStack.cpp

. - make cmake.

+6

- . main.cpp "Vector3DStack.cpp", Vector3DStack.h, Vector3DStack.h.

, , , , ( ) cpp ( ), ++ gotchas.. , , .

, , , , $0.02 .

C++!

+2
source

All Articles