I am working on a project involving mathematical vectors in three-dimensional space (not to be confused with the vector
collection type). I have a class Vector
defined in Vector.cpp
and declared in Vector.h
. My directory structure is as follows:
When I attempt to build the project, I get an LNK2019
unresolved external symbol error. As far as I can tell, all three of my files are on the build path.
In Vector.cpp
:
class Vector
{
private:
double xComponent;
double yComponent;
double zComponent;
public:
Vector(double x, double y, double z) : xComponent(x), yComponent(y), zComponent(z) {}
double dotProduct(const Vector& other) const
{
return xComponent * other.xComponent + yComponent * other.yComponent + zComponent * other.zComponent;
}
}
In Vector.h
:
#ifndef VECTOR_H
#define VECTOR_H
class Vector
{
public:
Vector(double x, double y, double z);
double dotProduct(const Vector& other) const;
}
#endif
In Vectors.cpp
:
#include "Vector.h"
#include <iostream>
using std::cout;
using std::endl;
int main()
{
Vector foo = Vector(3, 4, -7);
Vector bar = Vector(1.2, -3.6, 11);
cout << foo.dotProduct(bar) << endl;
return 0;
}
foo.dotProduct(bar)
is the only place where a linker error occurs (no error occurs on the constructor). I have tried some of the other non-constructor methods of Vector
and they also caused a linker error. Why does the constructor work but not any of the others?
This is the output from attempting to build the project:
1>------ Build started: Project: Vectors, Configuration: Debug Win32 ------
1>Vectors.obj : error LNK2019: unresolved external symbol "public: double __thiscall Vector::dotProduct(class Vector const &)const " (?dotProduct@Vector@@QBENABV1@@Z) referenced in function _main
1>C:\Users\John\Documents\Visual Studio 2017\Projects\Vectors\Debug\Vectors.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Vectors.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========