I am quite new at programming in C++… I started out programming in C# a year ago so I’m failing on some really basic stuff… I think…
I created a class called vector that is a point in 3d space the header of which looks like this:
class vec3
{
double x, y, z;
public:
vec3();
vec3(double x, double y, double z);
double calculateLLength(void);
double distance(double x1, double y1, double z1);
double distance(const vec3 &vec);
~vec3(void);
};
after this I was supposed to write a class called sphere which centerpoint is
a vector from my vector class which looks lite this (sphere header):
#pragma once
#include "vec3.h"
class Sphere
{
vec3 center1;
double x, y, z;
double radius1;
static const double PI;
public:
//constructs
Sphere(double x, double y, double z, double radius);
Sphere(vec3 center1, double radius);
//Methods
bool inside(const vec3 ¢er);
bool overlap(const Sphere &sphere);
double area();
double volume();
~Sphere(void);
};
the error I get is:
error LNK1120: 1 unresolved externals
I tried to Google this and spent most of my Saturday trying to fix it but can’t….
(here is the cpp files if anyone needs them!)
Sphere.cpp:
#include "StdAfx.h"
#include "Sphere.h"
#include "vec3.h"
#include <math.h>
const double Sphere::PI = 3.1415926535;
Sphere::Sphere(double centerX, double centerY, double centerZ, double radius)
{
vec3 center(centerX, centerY, centerZ);
radius1 = radius;
}
Sphere::Sphere(vec3 center, double radius)
{
center1 = center;
radius1 = radius;
}
bool Sphere::inside(const vec3 ¢er)
{
if(radius1 < center1.distance(center))
return false;
else
return true;
}
bool Sphere::overlap(const Sphere &sphere)
{
if(this->center1.distance(sphere.center1) < radius1 + sphere.radius1)
return true;
else
return false;
}
double Sphere::area()
{
}
double Sphere::volume()
{
}
Sphere::~Sphere(void)
{
}
vec3.cpp:
#include "StdAfx.h"
#include "vec3.h"
#include <math.h>
vec3::vec3(double x, double y, double z)
{
this->x=x;
this->y=y;
this->z=z;
}
double vec3::calculateLLength()
{
return sqrt((x*x) + (y*y) + (z*z));
}
double vec3::distance(double x1, double y1, double z1)
{
return sqrt( ((this->x - x1)*(this->x - x1)) + ((this->y - y1)*(this->y - y1)) + ((this->z - z1)*(this->z - z1)) );
}
double vec3::distance(const vec3 &vec)
{
return sqrt( ((this->x - vec.x)*(this->x - vec.x)) + ((this->y - vec.y)*(this->y - vec.y)) + ((this->z - vec.z)*(this->z - vec.z)) );
}
vec3::~vec3(void)
{
}
The issue is you have declared default constructor of vec3 but you haven’t provide a definition for it. Add below code to vec3.cpp:
vec3.cpp:
Side note,
Sphere::areaandSphere::volumeneed to return value as they are declare to returndouble: